gpt4 book ai didi

python pandas dataframe 添加带有过滤条件的总列

转载 作者:行者123 更新时间:2023-12-01 08:33:54 24 4
gpt4 key购买 nike

我有一个文件,可以在其中比较基础数据集不同 View 的不同信息。目标是列出信息片段并比较总数。

我有以下数据框:

df = pandas.DataFrame({"Measures": 
['Country','State','County','City'],
"Green": ['Included','Excluded','Included','Included'], "Orange":
['Excluded', 'Excluded', 'Excluded', 'Included']})

我有以下基础数据集:

Location    Green    Orange
Country 1 6
State 3 10
County 2 15
City 5 20

我希望最终的结果是这样的:

Measures    Green    Orange
Country Included Excluded
State Excluded Excluded
County Included Excluded
City Included Included
Total 8 20

最佳答案

您可以在计算总和之前使用 df 屏蔽底层数据帧的值。

m = df.eq('Included')   
# Assume df2 is your underlying DataFrame.
v = df2[m].sum()
# Assign the total back as a new row in df.
df.loc['Total', :] = v[df2.dtypes != object]

df
Measures Green Orange
0 Country Included Excluded
1 State Excluded Excluded
2 County Included Excluded
3 City Included Included
Total NaN 8 20
<小时/>

如果您想要更相同的输出,另一个选项是分别将“Measures”和“Locations”设置为索引。

df = df.set_index('Measures')
df2 = df2.set_index('Location')

m = df.eq('Included')
v = df2[m].sum()
df.loc['Total', :] = v

df
Green Orange
Measures
Country Included Excluded
State Excluded Excluded
County Included Excluded
City Included Included
Total 8 20

关于python pandas dataframe 添加带有过滤条件的总列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53808834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com