Read specific columns from a csv file with csv module?

后端 未结 12 1153
闹比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:44

    SAMPLE.CSV
    a, 1, +
    b, 2, -
    c, 3, *
    d, 4, /
    column_names = ["Letter", "Number", "Symbol"]
    df = pd.read_csv("sample.csv", names=column_names)
    print(df)
    OUTPUT
      Letter  Number Symbol
    0      a       1      +
    1      b       2      -
    2      c       3      *
    3      d       4      /
    
    letters = df.Letter.to_list()
    print(letters)
    OUTPUT
    ['a', 'b', 'c', 'd']
    

提交回复
热议问题