question.it
question.it

Reputation: 2978

Display dataframe column headers as a Pandas series

I have some data frame with 1050 columns, how to display column name in single column view.

list(df) # will give all column name in list, but i need to see in column
['FIPST', 'SECTOR', 'N07_EMPLOYER', 'RG', 'TABWGT', 'EMPLOYMENT_NOISY', 'PAYROLL_NOISY', 'RECEIPTS_NOISY', 'PCT1', 'PCT2', 'PCT3', 'PCT4', 'ETH1', ....]

Expected:

ColumnName
FIPST
SECTOR
N07_EMPLOYER
RG
TABWGT
EMPLOYMENT_NOISY

Tried:

df.columns.to_series()

but it showing in 2 column & won't show entire column list, in middle ... enter image description here

Upvotes: 0

Views: 6188

Answers (1)

cs95
cs95

Reputation: 402814

Option 1
To get a series/dataframe from column headers. You'd consider this option if you want to do more than just printing out those headers.

  1. First, change your display options, so more rows are displayed. You can do this be changing the max_rows attribute in pd.options:

    pd.options.display.max_rows = len(df.columns)
    
  2. Next, as Psidom suggested, use df.columns.to_series:

    print(df.columns.to_series().reset_index(drop=True))
    

Option 2
Just printing the column headers out. You can do this in one of two ways. One is as Jon Clements suggested:

print(*df.columns, sep='\n')

Alternatively, you could loop over df.columns as as Zero suggested:

for c in df.columns: print(c)

Upvotes: 1

Related Questions