Read in the first column of a CSV in python

前端 未结 5 1849
予麋鹿
予麋鹿 2020-12-07 02:04

I have a CSV (mylist.csv) with 2 columns that look similar to this:

jfj840398jgg     item-2f
hd883hb2kjsd     item-9k
jie9hgtrbu43     item-12
f         


        
5条回答
  •  不思量自难忘°
    2020-12-07 02:56

    You can also use pandas here:

    import pandas as pd
    df = pd.read_csv(mylist.csv)
    

    Then, getting the first column is as easy as:

    matrix2 = df[df.columns[0]].as_matrix()
    list2 = matrix2.tolist()
    

    This will return only the first column in list. You might want to consider leaving the data in numpy, if you're conducting further data operation on the result you get.

提交回复
热议问题