Pandas show column number

is there a way to get pandas to display the column number and column name at the same time? I am dealing with a dataset with s> 30 columns, all very long column names, and some with little variation with each other. Its unconditional task is to write out names when writing code. (I will still need to see the column names to know which columns to select)

thank.

+3


source to share


1 answer


One possible solution is to create MultiIndex

and then select columns DataFrame.xs

:



df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3

df.columns = pd.MultiIndex.from_arrays([pd.RangeIndex(len(df.columns)), df.columns])
print (df)
   0  1  2  3  4  5
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3

print (df.xs(2, level=0, axis=1))
   C
0  7
1  8
2  9

      

+4


source







All Articles