我有一个文件,用于比较基础数据集的不同视图的不同信息。目的是列出信息片段并比较总计。
我有以下数据框:
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
答案 0 :(得分:1)
在计算总和之前,您可以使用<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href='#' class='trigger'>Trigger</a>
<ul class='b3_archive_testimonial_category_list'>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
来掩盖基础数据框的值。
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