[英]How to make same value of one column for those rows which have same values in another column pandas
I have a pandas dataframe where some of the rows in a column named 'Case_num'
have the same value.我有一个 pandas dataframe ,其中名为
'Case_num'
的列中的某些行具有相同的值。 I want to fill the column named 'Work_Hrs'
with a sum of the non-zero values in that column if 'Case_num'
is the same.如果
'Case_num'
相同,我想用该列中非零值的总和填充名为'Work_Hrs'
的列。 For example, the input data is:例如,输入数据为:
I want to fill the column 'Work_Hrs'
with a sum of non zero values, ie 6 in this case, see below:我想用非零值的总和填充
'Work_Hrs'
列,在这种情况下为 6,见下文:
Thank you.谢谢你。
You can try the following: I have created a similar sample:您可以尝试以下操作:我创建了一个类似的示例:
dic={'Work':[0,1,2,4,1],'Case_Num':[1,1,1,3,3]}
df=pd.DataFrame(dic)
Work Case_Num
0 0 1
1 1 1
2 2 1
3 4 3
4 1 3
Grouping by the column and apply sum
on target column按列分组并在目标列上应用
sum
sum_series=df.groupby('Case_Num')['Work'].sum()
Case_Num
1 3
3 5
Name: Work, dtype: int64
And then create function to apply on dataframe然后创建 function 应用到 dataframe
def change_value(case,work):
if work==0:
work=sum_series[case]
return work
df['Work']=df.apply(lambda x: change_value(x['Case_Num'],x['Work']),axis=1)
Output: Output:
Work Case_Num
0 3 1
1 1 1
2 2 1
3 4 3
4 1 3
Update For changing Work
column with sum everywhere, you can use following更新要在任何地方使用 sum 更改
Work
列,您可以使用以下
df['Work']=df['Case_Num'].apply(lambda x: sum_series[x])
Hope it helps.希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.