Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas dataframe to_csv works with sep='\n' but not sep='\t'

I try to print my large dataframe to csv file but the tab separation sep='\t' does not work. I then test with newline sep='\n', it seems work ok, break all the elements by newline. What are possibly wrong here?

The code is so simple like

df_M.to_csv('report'+filename, header=True, sep='\t', index=False)

The example of data (the protein column is very long), I mark where to separate by |

"protein |  cl      | pept |    [M] |  [M+1H+]1+ |  [M+2H+]2+"      
"ALBU_HUMAN_UPS Serum albumin (Chain 26-609) - Homo sapiens (Human)|    0|   AWAVAR|        672.37072|            673.378| out-of-range"        
"ALBU_HUMAN_UPS Serum albumin (Chain 26-609) - Homo sapiens (Human)|    0|  TPVSDR| 673.3394900000002|  674.3467700000002|  out-of-range"       
"ALBU_HUMAN_UPS Serum albumin (Chain 26-609) - Homo sapiens (Human)|    0|  NYAEAK| 694.3285900000001|  695.3358700000001|  out-of-range"       
"
like image 460
Jan Avatar asked Nov 16 '25 05:11

Jan


1 Answers

Are you saving the data as .tsv format? your data is tsv file as you are separating the data with '\t' which is tab. csv file must be separated by ",".

If you wanna save data with .csv format you need to separate by ","

Link for .csv RFC. http://www.ietf.org/rfc/rfc4180.txt

like image 53
T_cat Avatar answered Nov 17 '25 19:11

T_cat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!