Home > database >  Pandas How to rename columns that don't have names, but they're indexed as 0, 1, 2, 3... e
Pandas How to rename columns that don't have names, but they're indexed as 0, 1, 2, 3... e

Time:11-13

I don't know how to rename columns that are unnamed. I have tried both approaches where I am putting the indices in quoutes and not, like this, and it didn't work:

    train_dataset_with_pred_new_df.rename(columns={
    0 : 'timestamp', 1 : 'open', 2 : 'close', 3 : 'high', 4 : 'low', 5 : 'volume', 6 : 'CCI7', 7 : 'DI ',\
    8 : 'DI-', 9 : 'ADX', 10 : 'MACD Main', 11 : 'MACD Signal', 12 : 'MACD histogram', 13 : 'Fisher Transform',\
    14 : 'Fisher Trigger'
})

And

    train_dataset_with_pred_new_df.rename(columns={
    '0' : 'timestamp', '1' : 'open', '2' : 'close', '3' : 'high', '4' : 'low', '5' : 'volume', '6' : 'CCI7', '8' : 'DI ',\
    '9' : 'DI-', '10' : 'ADX', '11' : 'MACD Main', '12' : 'MACD Signal', '13' : 'MACD histogram', '15' : 'Fisher Transform',\
    '16' : 'Fisher Trigger'
})

So If both didn't worked, how do I rename them?

Thank you for your help in advance :)

CodePudding user response:

pandas.DataFrame.rename returns a new DataFrame if the parameter inplace is False.

You need to reassign your dataframe :

train_dataset_with_pred_new_df= train_dataset_with_pred_new_df.rename(columns={
0 : 'timestamp', 1 : 'open', 2 : 'close', 3 : 'high', 4 : 'low', 5 : 'volume', 6 : 'CCI7', 7 : 'DI ',\
8 : 'DI-', 9 : 'ADX', 10 : 'MACD Main', 11 : 'MACD Signal', 12 : 'MACD histogram', 13 : 'Fisher Transform',\
14 : 'Fisher Trigger'})

Or simply use inplace=True:

train_dataset_with_pred_new_df.rename(columns={
    0 : 'timestamp', 1 : 'open', 2 : 'close', 3 : 'high', 4 : 'low', 5 : 'volume', 6 : 'CCI7', 7 : 'DI ',
    8 : 'DI-', 9 : 'ADX', 10 : 'MACD Main', 11 : 'MACD Signal', 12 : 'MACD histogram', 13 : 'Fisher Transform',
    14 : 'Fisher Trigger'
}, inplace=True)

CodePudding user response:

df.rename(columns={ df.columns[1]: "your value" }, inplace = True)

CodePudding user response:

What you are trying to do is renaming the index. Instead of renaming existing columns you are renaming index. So rename index and not columns.

train_dataset_with_pred_new_df.rename(
    index={ 0 : 'timestamp', 1 : 'open', 2 : 'close', 3 : 'high', 4 : 'low', 5 : 'volume', 6 : 'CCI7', 7 : 'DI ', 8 : 'DI-', 9 : 'ADX', 10 : 'MACD Main', 11 : 'MACD Signal', 12 : 'MACD histogram', 13 : 'Fisher Transform', 14 : 'Fisher Trigger'
}, inplace=True)

CodePudding user response:

As it looks like you want to reassign all names, simply do:

df.columns = ['timestamp', 'open', 'close', 'high', 'low', 'volume',
              'CCI7', 'DI ', 'DI-', 'ADX', 'MACD Main', 'MACD Signal',
              'MACD histogram', 'Fisher Transform', 'Fisher Trigger']

Or, in a chain:

df.set_axis(['timestamp', 'open', 'close', 'high', 'low', 'volume',
             'CCI7', 'DI ', 'DI-', 'ADX', 'MACD Main', 'MACD Signal',
             'MACD histogram', 'Fisher Transform', 'Fisher Trigger'],
             axis=1)
  • Related