Delete specific columns from csv file in python3

后端 未结 1 529
忘了有多久
忘了有多久 2021-01-14 22:17

Need to delete some specific column and rows(by index) of multiple csv files, without creating new files.

For the code below, it is giving output with new blank rows

1条回答
  •  借酒劲吻你
    2021-01-14 23:00

    Since you wish not to generate any new csv files and would like the data to perform operations, I would suggest you to make use of Pandas Framework. Make use of drop function in this framework.

    Consider the following example:

    Sample.csv:

    col1,col2,col3,col4
    1,2,3,4
    5,6,7,8
    9,10,11,12
    13,14,15,16
    17,18,19,20
    

    Code:

    import pandas as pd
    df = pd.read_csv('./Sample.csv')
    

    To delete columns:

    df.drop('col3', axis = 1, inplace = True)
    

    df contents:

       col1     col2    col4
    0   1   2   4
    1   5   6   8
    2   9   10  12
    3   13  14  16
    4   17  18  20
    

    To delete rows:

    df.drop(df.index[[1,4]], inplace = True)
    

    df contents:

       col1     col2    col4
    0   1   2   4
    2   9   10  12
    3   13  14  16
    

    Finally to save the edited csv file:

    df.to_csv('new_sample.csv', index = False)
    

    0 讨论(0)
提交回复
热议问题