Home > front end >  rename column name `pd.NaT` in pandas python
rename column name `pd.NaT` in pandas python

Time:10-17

I have a dataframe with columns. enter image description here

trxmonth    NaT 2017-07-01  2017-08-01  2017-09-01  2017-10-01  2017-11-01  2017-12-01  2018-01-01  2018-02-01  2018-03-01  ... 2021-12-01  2022-01-01  2022-02-01  2022-03-01  2022-04-01  2022-05-01  2022-06-01  2022-07-01  2022-08-01

I use reset_index for remove value as an index and add into columns and it works fine but column name is Nat. I try to change Nat-> customer_name but it doesn't change.

I use this code: CODE:

df.rename(columns={pd.NaT: "customer_name"})

but it can effect others column too. enter image description here

Simply I want to change NaT to customer_name. I also use this link but it cannot help me. Any help will be appreciated.

CodePudding user response:

Use:

c = pd.DatetimeIndex([ 'NaT', '2017-07-01', '2017-08-01', '2017-09-01', '2017-10-01', '2017-11-01', '2017-12-01', 
                      '2018-01-01', '2018-02-01', '2018-03-01'],
                       dtype='datetime64[ns]', name='trxmonth', freq=None)
df = pd.DataFrame(columns=c, index=[0])

First convert columns names to strings YYYY-MM-DD for avoid added 00:00:00 times if mixed columns names - datetimes and string customer_name:

#default datetimes has 00:00:00 times
print (df.columns.tolist())
[NaT, Timestamp('2017-07-01 00:00:00'), Timestamp('2017-08-01 00:00:00'), Timestamp('2017-09-01 00:00:00'),
 Timestamp('2017-10-01 00:00:00'), Timestamp('2017-11-01 00:00:00'), Timestamp('2017-12-01 00:00:00'),
 Timestamp('2018-01-01 00:00:00'), Timestamp('2018-02-01 00:00:00'), Timestamp('2018-03-01 00:00:00')]

df.columns = df.columns.strftime('%Y-%m-%d')
df = df.rename(columns={np.nan: "customer_name"})
print (df)
trxmonth customer_name 2017-07-01 2017-08-01 2017-09-01 2017-10-01 2017-11-01  \
0                  NaN        NaN        NaN        NaN        NaN        NaN   

trxmonth 2017-12-01 2018-01-01 2018-02-01 2018-03-01  
0               NaN        NaN        NaN        NaN  

Or convert values to dates:

df.columns = df.columns.date
df = df.rename(columns={pd.NaT: "customer_name"})
print (df)
  customer_name 2017-07-01 2017-08-01 2017-09-01 2017-10-01 2017-11-01  \
0           NaN        NaN        NaN        NaN        NaN        NaN   

  2017-12-01 2018-01-01 2018-02-01 2018-03-01  
0        NaN        NaN        NaN        NaN  

CodePudding user response:

For me this works:

df = pd.DataFrame([[1,2,3,4]], columns=[pd.NaT, '2017-07-01', '2017-08-01', '2017-09-01'])
df.rename(columns={pd.NaT: "customer_name"})

Output:

   customer_name  2017-07-01  2017-08-01  2017-09-01
0              1           2           3           4

CodePudding user response:

Try to change column header by index, such as df.columns.values[n], additionally to get index position dynamically you can try to run loop and check if its NaT or not.

As your column names types are datetime, try to put condition where you will compare the type.

Code:

df.columns.values[[i for i,d in enumerate(df.columns) if type(d)==type(pd.NaT)]] = 'customer_name'
df.columns

Ouput:

Index([' ', 2015-02-17 16:53:25, 'customer_name'], dtype='object')

CodePudding user response:

try recreating the columns as follows, first convert to string and then get the column names excluding NaT, add ['customer_name'] in the place you want it.

df.columns = ['customer_name'] df.columns.astype(str).tolist()[1:]

  • Related