Reputation: 141
I have the following code:
df_demo['Age'] = df_demo['Age'].replace([23842674135270370,
23842674044440370, 23842674044420370, 23842674044430370],
['18-24', '25-34', '35-44', '45+'])
(The numbers are ad id tags, and I'm trying to replace them to the age groups they are targeting.)
The code is only reading the first number and replacing it (to 18-24). The rest of the numbers are not reading and replacing. If I flip the order of the numbers (like move the 25-34 pairing to the first set) it replaces that first pairing but none of the others.
I have exactly the same construction for .replace() -- using two lists within the () -- further up in my program and it's working perfectly. But this one is not, and I can't figure out why it is not working.
Upvotes: 1
Views: 183
Reputation: 863216
For me working convert column Age
to string
by dtype
and then replace string
s by another one:
df_demo = pd.read_csv('demographics - Sheet1.csv', dtype={'Age':str})
print (df_demo.tail())
190 191 23842674135270370 Yes
191 192 23842674135270370 Yes
192 193 23842674044420370 Yes
193 194 23842674135270370 Yes
194 195 23842674044420370 Yes
df_demo['Age'] = df_demo['Age'].replace(
['23842674135270370','23842674044440370','23842674044420370','23842674044430370'],
['18-24', '25-34', '35-44', '45+'])
print (df_demo.tail())
Name Age Newsletter
190 191 18-24 Yes
191 192 18-24 Yes
192 193 35-44 Yes
193 194 18-24 Yes
194 195 35-44 Yes
Upvotes: 1