I have a dataframe with three columns- Col1
, Col2
and Col3
Col1 | Col2 | Col3 |
---|---|---|
x | a | 10 |
x | b | 12 |
x | c | 25 |
y | a | 13 |
y | b | 14 |
y | c | 37 |
I want to update the value of Col3
for each c
from Col2
for each unique Col1
values by the below logic:
c-(a+b)
The desired output:
Col1 | Col2 | Col3 |
---|---|---|
x | a | 10 |
x | b | 12 |
x | c | 3 |
y | a | 13 |
y | b | 14 |
y | c | 10 |
I would like to know the appropriate operation that I need to do to achieve the desired solution. Thanks
We can select the subset of rows where the Col2
values is either a
or b
, then group these rows by Col1
and transform using sum
to calculate the transformed sum a + b
per group, finally subtract the transformed sum from Col3
where the corresponding Col2
is c
m = df['Col2'].isin(['a', 'b'])
s = df['Col3'].where(m).groupby(df['Col1']).transform('sum')
df.loc[df['Col2'].eq('c'), 'Col3'] -= s
Col1 Col2 Col3
0 x a 10.0
1 x b 12.0
2 x c 3.0
3 y a 13.0
4 y b 14.0
5 y c 10.0
Might be verbose, but a possible way if each group has only one a
, b
and c
.
def subtract(group):
result = group.loc[df['Col2'] == 'c', 'Col3'].values - (group.loc[df['Col2'] == 'a', 'Col3'].values + group.loc[df['Col2'] == 'b', 'Col3'].values)
group.loc[df['Col2'] == 'c', 'Col3'] = result
return group
df_ = df.groupby('Col1').apply(subtract)
print(df_)
Col1 Col2 Col3
0 x a 10
1 x b 12
2 x c 3
3 y a 13
4 y b 14
5 y c 10
Well, If you wanna perform this kind of operation. Why not restructure the data?. You can use pivot_table ->
df1 = df.pivot_table(index = 'Col1',columns='Col2', values='Col3')
print(df1)
Output-
Col2 a b c
Col1
x 10 12 25
y 13 14 37
Then you can perform the operations that you want easily and after that, if you want data back in its original format you can always use the melt
function.
df1['c'] -= df1['a']+ df1['b']
df1.melt()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.