trying to out put a dataframe into txt file (for a feed). a few specific columns are getting automatically converted into float instead of int as intented.
how can i specific those columns to use int as dtype?
i tried to output the whole dataframd as string and that did not work.
the columns i would like to specify are named [CID1] and [CID2]
data = pd.read_sql(sql,conn)
data = data.astype(str)
data.to_csv('data_feed_feed.txt', sep ='\t',index=True)
Based on the code you provided, you turn all of your data to strings just before export.
Thus, you either need to turn some cols back to desired type, such as:
data["CID1"] = data["CID1"].astype(int)
or not convert them in the first place.
It is not clear from what you provided why you'd have issues with ints being converted to floats.
this post provides heaps of info:
stackoverflow.com/a/28648923/9249533
I am very new to programming and am working on an Arduino project to measure wavelengths of light.
I am using the Spectruino 3 and am trying to read in the bytes that it records into an array, convert the array into an integer array and then export that data to excel. Currently I have this:
const int numBytes = 501;
int bytestream[numBytes];
void setup(){
Serial.begin(115200);
}
void loop() {
for(int i=0; i<numBytes; i++){
bytestream[i]=Serial.read();
}
}
My spectrometer currently doesn't work, but this seems to compile. I was just wondering how to convert this bytestream array into an integer array and then export it into Excel.
Excel and other, better spreadsheet programs can load text file based CSV files.
That means you have 2 options:
Convert data to CSV rows in device.
Send text data over serial line and use any serial port program on PC that can receive to file to catch the data.
Open CSV file in Excel or other, better spreadsheet program.
Or
Send raw binary data over serial line and use any serial port program on PC that can receive to file to catch the data.
Convert data to CSV file with your favorite scripting language (like Python).
Open CSV file in Excel or other, better spreadsheet program.
Am using androidplot library to display dynamic/static charts & graphs within my Android app. But now I have to export those charts/graphs into excel formats. AndroidPlot library is not providing any API to export the charts/graphs. Is there anyway to do the same?
Can anybody please help me or let me know some workaround to deal with this issue.
That is not a built-in feature of Androidplot however it should be trivial to write a method to suit your needs. All you would need to do is iterate over the XYSeries and write each x/y value to a file separated by a comma. This gives you a generic CSV formatted file that Excel can open. Whether the values or interleaved in or series, how many series are contained in a single file etc, is up to you. To get you started, here's a simple routine that will convert a single XYSeries to an interleaved CSV string. All that's left to do is write the String to file:
/**
* Converts an XYSeries into CSV "x,y,x,y..." format.
* #param series The series to be converted.
* #return CSV formatted version of series.
*/
String toCsv(XYSeries series) {
StringBuffer output = new StringBuffer();
for(int i = 0; i < series.size(); i++) {
output.append(series.getX(i).toString());
output.append(',');
output.append(series.getY(i)).toString();
}
return output.toString();
}
I don't know if Matlab can do this, but I want to store some strings in a 4×3 matrix, each element in the matrix is a string.
test_string_01 test_string_02 test_string_03
test_string_04 test_string_05 test_string_06
test_string_07 test_string_08 test_string_09
test_string_10 test_string_11 test_string_12
Then, I want to write this matrix into a plain text file, either comma or space delimited.
test_string_01,test_string_02,test_string_03
test_string_04,test_string_05,test_string_06
test_string_07,test_string_08,test_string_09
test_string_10,test_string_11,test_string_12
Seems like matrix data type is not capable of storing strings. I looked at cell. I tried to use dlmwrite() or csvwrite(), but both of them only accept matrices. I also tried cell2mat() first, but in that way all letters in the strings are comma seperated, like
t,e,s,t,_,s,t,r,i,n,g,_,0,1,t,e,s,t,_,s,t,r,i,n,g,_,0,2,t,e,s,t,_,s,t,r,i,n,g,_,0,3
So is there any way to achieve this?
It is possible to shorten yuk's solution a bit.
strings = {
'test_string_01','test_string_02','test_string_03'
'test_string_04','test_string_05','test_string_06'
'test_string_07','test_string_08','test_string_09'
'test_string_10','test_string_11','test_string_12'};
fid = fopen('output.txt','w');
fmtString = [repmat('%s\t',1,size(strings,2)-1),'%s\n'];
fprintf(fid,fmtString,strings{:});
fclose(fid);
Cell array is the way to store strings.
I agree it's a pain to save strings into a text file, but you can do it with this code:
strings = {
'test_string_01','test_string_02','test_string_03'
'test_string_04','test_string_05','test_string_06'
'test_string_07','test_string_08','test_string_09'
'test_string_10','test_string_11','test_string_12'};
fid = fopen('output.txt','w');
for row = 1:size(strings,1)
fprintf(fid, repmat('%s\t',1,size(strings,2)-1), strings{row,1:end-1});
fprintf(fid, '%s\n', strings{row,end});
end
fclose(fid);
Substitute \t with , to get csv file.
You can also store cell array of strings into Excel file with XLSWRITE (requires COM interface, so it's on Windows only):
xlswrite('output.xls',strings)
In most cases you can use the delimiter ' ' and get Matlab to save a string into file with dlmwrite.
For example,
output=('my_first_String');
dlmwrite('myfile.txt',output,'delimiter','')
will save a file named myfile.txt containing my_first_String.