Can pandas read a transposed CSV?

后端 未结 2 1344
灰色年华
灰色年华 2021-01-04 08:30

Can pandas read a transposed CSV? Here\'s the file (note I\'d also like to select a subset of columns):

A,x,x,x,x,1,2,3
B,x,x,x,x,4,5,6
C,x,x,x,         


        
相关标签:
2条回答
  • 2021-01-04 08:53

    In addition, if your file looks like this:

    "some-line-you-want-to-skip"
    A,x,x,x,x,1,2,3
    B,x,x,x,x,4,5,6
    C,x,x,x,x,7,8,9
    

    It is possible to do the following:

    df = pd.read_csv(filename, skiprows=1, header=None).T   # Read csv, and transpose
    df.columns = df.iloc[0]                                 # Set new column names
    df.drop(0,inplace=True)                                 # Drop duplicated row
    

    This will also end up with the df looking the way you want

    0 讨论(0)
  • 2021-01-04 08:59
    pd.read_csv('file.csv', index_col=0, header=None).T
    
    0 讨论(0)
提交回复
热议问题