Pandas How To Replace Values Based On Conditions For Several Values
I have a Dataframe with several column and below is first 3 columns in that dataframe: data_df = pd.DataFrame({'id':['era','bb','cs','jd','ek','gtf','okg','huf','mji','loj','djjf',
Solution 1:
Create a dict for mapping -
dict1 = dict(zip(range(1, 11), range(10,0,-1)))
data_df['dif'] = data_df['dif'].map(dict1)
Solution 2:
new_df = data_df.assign(dif=11 - data_df['dif'])
Or, if you want to do it in place:
data_df['dif'] = 11 - data_df['dif']
Solution 3:
It's really simple:
>>> data_df["dif"] = 11 - data_df["dif"]
>>> data_df
id doc dif
0 era 1050 10
1 bb 580 10
2 cs 170 10
3 jd 8 8
4 ek 7 8
5 gtf 220 9
6 okg 45155 9
7 huf 305 8
8 mji 458 7
9 loj 201 6
10 djjf 48 3
11 wloe 78 4
12 rfm 256 2
13 cok 358 1
or replace 11 by data_df["dif"].max() + 1
Post a Comment for "Pandas How To Replace Values Based On Conditions For Several Values"