Home > Net >  Pandas_Read date only as column header
Pandas_Read date only as column header

Time:01-27

I have a DataFrame read from excel and the column headers are read in datetime format. I just want the column headers to be read only in date format without the timestamp.

col header  2017-01-31 00:00:00  2017-02-28 00:00:00    2025-03-31 00:00:00  2025-04-30 00:00:00

Row1           100                  200                    300                 400
Row2           200                  300                    400                 500

Expected output is

col header  2017-01-31   2017-02-28     2025-03-31  2025-04-30 

Row1           100           200            300         400
Row2           200           300            400         500

CodePudding user response:

You need to process the header after import.

Assuming, the first "column" is actually the index, I suggest two options.

As string:

df.columns = df.columns.map(str.split).str[0]

As datetime type:

df.columns = pd.to_datetime(df.columns)

If "col header" is actually a column, set it as index temporarily: df = df.set_index('col header'), then update the headers and reset_index: df = df.set_index('col header')

CodePudding user response:

If the column names in header are datetime objects;

df.columns = [col.dt.date for col in df.columns]
  •  Tags:  
  • Related