Given the following dataframe,
INPUT df:
Cost_centre | Pool_costs |
---|---|
90272 | A |
92705 | A |
98754 | A |
91350 | A |
Replace Pool_costs value with ‘B’ given the Cost_centre value but keep the Pool_costs value if the Cost_centre value does not appear in list.
OUTPUT df:
Cost_centre | Pool_costs |
---|---|
90272 | B |
92705 | A |
98754 | A |
91350 | B |
Current Code:
This code works up until the else side of lambda; finding the Pool_costs value again is the hard part.
JavaScript
x
10
10
1
df = pd.DataFrame({'Cost_centre': [90272, 92705, 98754, 91350],
2
'Pool_costs': ['A', 'A', 'A', 'A']})
3
4
pool_cc = ([90272,91350])
5
6
pool_cc_set = set(pool_cc)
7
8
df['Pool_costs'] = df['Cost_centre'].apply(lambda x: 'B' if x in pool_cc_set else df['Pool_costs'])
9
print (df)
10
I have used the following and have found success but it gets hard to read and modify when there are a lot of cost_centre’s to change.
JavaScript
1
6
1
df = pd.DataFrame({'Cost_centre': [90272, 92705, 98754, 91350],
2
'Pool_costs': ['A', 'A', 'A', 'A']})
3
4
filt = df['Cost_centre'] == '90272'|df['Cost_centre'] == '91350')
5
df.loc[filt, 'Pool_costs'] = 'B'
6
Advertisement
Answer
IIUC, you can use isin
JavaScript
1
3
1
filt = df['Cost_centre'].isin([90272, 91350])
2
df.loc[filt, 'Pool_costs'] = 'B'
3
JavaScript
1
8
1
print(df)
2
3
Cost_centre Pool_costs
4
0 90272 B
5
1 92705 A
6
2 98754 A
7
3 91350 B
8