Ivan Shelonik
Ivan Shelonik

Reputation: 2028

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. How to load it clearly?

Upvotes: 15

Views: 10017

Answers (2)

EdChum
EdChum

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

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210882

try this:

pd.read_csv(file_name, nrows=1).columns.tolist()

Upvotes: 21

Related Questions