How can I read only the header column of a CSV file using Python?

后端 未结 9 685
生来不讨喜
生来不讨喜 2020-12-14 10:05

I am looking for a a way to read just the header row of a large number of large CSV files.

Using Pandas, I have this method available, for each csv file:

         


        
相关标签:
9条回答
  • 2020-12-14 10:39

    it is easy you can use this:

    df = pd.read_csv("path.csv", skiprows=0, nrows=2)
    df.columns.to_list()
    

    In this case you can only read really few row for get your header

    0 讨论(0)
  • 2020-12-14 10:40

    Here's one way. You get 1 row.

    In [9]: DataFrame(np.random.randn(10,4),columns=list('abcd')).to_csv('test.csv',mode='w')
    
    In [10]: read_csv('test.csv',index_col=0,nrows=1)
    Out[10]: 
              a         b         c         d
    0  0.365453  0.633631 -1.917368 -1.996505
    
    0 讨论(0)
  • 2020-12-14 10:41
    import pandas as pd
    
    get_col = list(pd.read_csv("first_test_pipe.csv",sep="|",nrows=1).columns)
    print(get_col)
    
    0 讨论(0)
提交回复
热议问题