Home > Software engineering >  Replacing dictionary indices with specific names in Pandas
Replacing dictionary indices with specific names in Pandas

Time:07-15

Hi I have this dictionary

df = 
{
1: 0.03287410711253387, 
2: 0.1110746346378032, 
3: 0.23769202033121095, 
4: 0.6183592379186247
}

I'd like to replace the 1,2,3,4 with A,B,C,D

I have tried

df['A','B','C','D'] = df.pop[1,2,3,4]

it gives me this error

ValueError: Length mismatch: Expected axis has 414 elements, new values have 4 elements

CodePudding user response:

I don't know why it's tagged pandas but you can do:

dict(zip(['A','B','C','D'], df.values()))
  • Related