[英]How to load Only column names from csv file (Pandas)?
I have a large csv file and don't want to load it fully into my memory, I need to get only column names from this csv file. 我有一个大的csv文件,不想将它完全加载到我的内存中,我需要从这个csv文件中只获取列名。 How to load it clearly?
如何清楚地加载它?
尝试这个:
pd.read_csv(file_name, nrows=1).columns.tolist()
If you pass nrows=0
to read_csv
then it will only load the column row: 如果将
nrows=0
传递给read_csv
那么它只会加载列行:
In[8]:
import pandas as pd
import io
t="""a,b,c,d
0,1,2,3"""
pd.read_csv(io.StringIO(t), nrows=0)
Out[8]:
Empty DataFrame
Columns: [a, b, c, d]
Index: []
After which accessing attribute .columns
will give you the columns: 之后,访问属性
.columns
将为您提供以下列:
In[10]:
pd.read_csv(io.StringIO(t), nrows=0).columns
Out[10]: Index(['a', 'b', 'c', 'd'], dtype='object')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.