简体   繁体   English

如何为在另一列 pandas 中具有相同值的那些行使一列的值相同

[英]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.

相关问题 如何删除具有相同列功能值的行,以保留保留已删除行另一功能的所有值的一行? - How to delete rows that share the same value of a column feature to make one row which keeps all the values of another feature of the deleted rows? 如何组合 pandas dataframe 中在一列中具有相同值的行 - How to combine rows in a pandas dataframe that have the same value in one column 如何按列名过滤值,然后将具有相同值的行提取到另一个CSV文件? Python /熊猫 - How to filter values by Column Name and then extract the rows that have the same value to another CSV file? Python/Pandas Python:我有具有相同列名的 Pandas 数据框。 如何更改其中之一? - Python: I have pandas dataframe which has the same column name. How to change one of those? 从Pandas Dataframe中找到列中的唯一值,然后查看这些值在另一列中是否具有相同的值 - From Pandas Dataframe find unique values in column and see if those values have the same values in another column 熊猫:基于列值合并2个数据框; 对于包含相同列值的多个行,请将其附加到不同的列 - Pandas: Merge 2 dataframes based on a column values; for mulitple rows containing same column value, append those to different columns 从 Pandas Dataframe 中选择一列中具有相同值而另一列仅缺失的行 - Selecting rows from Pandas Dataframe with same values in one column that have only missing in another 如果它们在列中具有相同的值并且其中至少一个包含另一列中的字符串,如何保留多行 - How to keep multiple rows if they have the same value within a column AND at least one of them contains a string in another column 如何使一些熊猫列值默认为另一列但同一行中的另一个值? - How to make some pandas column values default to another value in a different column, but same row? 如何进行查询以过滤其中一列等于同一表的另一列的行? - How to make a query that filters rows in which one column equals another one of the same table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM