Dropping some columns when using to_csv in pandas

时间:2016-04-04 18:36:04

标签: python pandas

I have a data frame which I want to write to tow files, one that contains all of the columns and one that has only a subset of the columns So for this data frame:

 Out_data
 Out[9]: 
    A     B                 C          D       E         F
0  354   49985400          10          07   7.140899  0.212044
1  738   49985400          10          07   7.140899  0.212044
2  738   49985277          11          09   4.024423  0.098387
3  246   49985279          10          07   7.140899  0.212044

I want to have it exported to two csv files, one that has all the data, and the second one that has only data from columns A,B, C and D, so that the csv would look like this:

  A,B,C,D,E,F
354,49985400,10,07,7.140899,0.212044
738,49985400,10,07,7.140899,0.212044
738,49985277,11,09,4.024423,0.098387
246,49985279,10,07,7.140899,0.212044

And the second one will look like:

  A,B,C,D
354,49985400,10,07
738,49985400,10,07
738,49985277,11,09
246,49985279,10,07

I am able to get the first file using:

Out_data.to_csv(filename, mode = 'w', index=False)

I tried using

Out_data.to_csv(filename, mode = 'w', cols = ['A','B','C','D'] ,index=False)

But I still go the exact same output file? How can I get to_csv to export but drop some of the columns?

2 个答案:

答案 0 :(得分:2)

You should be using columns as the keyword argument, not cols.

Out_data.to_csv(filename, mode='w', columns=['A','B','C','D'], index=False)

答案 1 :(得分:1)

Have you tried:

Out_data[["A", "B", "C", "D"]].to_csv(filename, mode = 'w' ,index=False)