Writing a pandas DataFrame to CSV file

前端 未结 7 1824
死守一世寂寞
死守一世寂寞 2020-11-22 11:07

I have a dataframe in pandas which I would like to write to a CSV file. I am doing this using:

df.to_csv(\'out.csv\')

And getting the error

7条回答
  •  生来不讨喜
    2020-11-22 11:24

    When you are storing a DataFrame object into a csv file using the to_csv method, you probably wont be needing to store the preceding indices of each row of the DataFrame object.

    You can avoid that by passing a False boolean value to index parameter.

    Somewhat like:

    df.to_csv(file_name, encoding='utf-8', index=False)
    

    So if your DataFrame object is something like:

      Color  Number
    0   red     22
    1  blue     10
    

    The csv file will store:

    Color,Number
    red,22
    blue,10
    

    instead of (the case when the default value True was passed)

    ,Color,Number
    0,red,22
    1,blue,10
    

提交回复
热议问题