简体   繁体   English

使用熊猫读取第 0 行 csv 并将其保存到列表中

[英]using pandas read 0th row of csv and save it into list

this is my dataframe:-这是我的数据框:-

A            B            C           D
1            23           34          23
3            23           21           4
4            5            6            7

what i want to do is read 0th row and add it into a list like this [A,B,C,D]我想要做的是读取第 0 行并将其添加到这样的列表中[A,B,C,D]

(here this column name can be anything) (这里这个列名可以是任何东西)

I have used我用过了

data = pd.read_csv(file_path, nrows=0)

But i am not able to add/store it into list但我无法将其添加/存储到列表中

How to do that?怎么做?

You can easily do this:你可以很容易地做到这一点:

list(data)

but it is more explicit to write like t his:但像他这样写更明确:

list(data.columns.values)

Edit: As RafAelC said correctly it is better to use tolist() like t his:编辑:正如 RafAelC 所说的那样,最好使用像他这样的tolist()

data.columns.values.tolist()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM