1

I have a similar doubt to the one in the mentioned link. Instead of returning column names in a list, I want column names in the format dtype:object. For example,

A
B
C
D
Name:x,dtype:object

I am using Excel file in xlsx format.

Link: Get list from pandas DataFrame column headers

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
cliff
  • 45
  • 2
  • 5

1 Answers1

0

I think you need read_excel first for df and then Series constructor or Index.to_series for Series from column names:

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

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

s = pd.Series(df.columns.values, name='x')
print (s)
0    A
1    B
2    C
3    D
Name: x, dtype: object

s1 = df.columns.to_series().rename('x')
print (s1)
A    A
B    B
C    C
D    D
Name: x, dtype: object
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252