Reputation: 2028
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. How to load it clearly?
Upvotes: 15
Views: 10017
Reputation: 394159
If you pass nrows=0
to read_csv
then it will only load the column row:
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:
In[10]:
pd.read_csv(io.StringIO(t), nrows=0).columns
Out[10]: Index(['a', 'b', 'c', 'd'], dtype='object')
Upvotes: 11
Reputation: 210882
try this:
pd.read_csv(file_name, nrows=1).columns.tolist()
Upvotes: 21