Home > Software engineering >  Copy an N-Index column of a Pandas Dataframe including all sub-headers
Copy an N-Index column of a Pandas Dataframe including all sub-headers

Time:07-28

This question expands off of a previous question of mine: Copy a Multi-Index column of a Pandas Dataframe including the second header

EDIT - Clarifying that one of my issues deals with the discrepancy when N=1 (produces an Index dataframe) and when N>1 (produces a MultiIndex dataframe...)

Background - I have a dataset with N headers that I've read from a CSV. N can be anything greater than or equal to 1...

e.g. N=1:

df = pd.read_csv(file, header=[0:N])
print(df)

     A   B   C 
---------------  (Single-)Index dataframe
0    1   2   3
1    4   5   6
2    7   8   9
       ...

e.g. N=3:

df = pd.read_csv(file, header=[0:N])
print(df)

     A   B   C
     a   b   c
     ɑ   β              
  • Related