Read specific columns from a csv file with csv module?

后端 未结 12 1154
闹比i
闹比i 2020-11-22 10:06

I\'m trying to parse through a csv file and extract the data from only specific columns.

Example csv:

ID | N         


        
12条回答
  •  我在风中等你
    2020-11-22 10:48

    Use pandas:

    import pandas as pd
    my_csv = pd.read_csv(filename)
    column = my_csv.column_name
    # you can also use my_csv['column_name']
    

    Discard unneeded columns at parse time:

    my_filtered_csv = pd.read_csv(filename, usecols=['col1', 'col3', 'col7'])
    

    P.S. I'm just aggregating what other's have said in a simple manner. Actual answers are taken from here and here.

提交回复
热议问题