How to delete columns in a CSV file?

后端 未结 9 1722
你的背包
你的背包 2020-11-27 18:05

I have been able to create a csv with python using the input from several users on this site and I wish to express my gratitude for your posts. I am now stumped and will po

9条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 18:18

    Use of Pandas module will be much easier.

    import pandas as pd
    f=pd.read_csv("test.csv")
    keep_col = ['day','month','lat','long']
    new_f = f[keep_col]
    new_f.to_csv("newFile.csv", index=False)
    

    And here is short explanation:

    >>>f=pd.read_csv("test.csv")
    >>> f
       day  month  year  lat  long
    0    1      4  2001   45   120
    1    2      4  2003   44   118
    >>> keep_col = ['day','month','lat','long'] 
    >>> f[keep_col]
        day  month  lat  long
    0    1      4   45   120
    1    2      4   44   118
    >>>
    

提交回复
热议问题