14

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?

Ivan Shelonik
  • 1,958
  • 5
  • 25
  • 49

2 Answers2

19

try this:

pd.read_csv(file_name, nrows=1).columns.tolist()
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
10

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')
EdChum
  • 376,765
  • 198
  • 813
  • 562