Python 如何使用 Pandas 的 DataFrame 计算百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23539832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate percentage with Pandas' DataFrame
提问by user977828
How to add another column to Pandas' DataFrame with percentage? The dict can change on size.
如何以百分比向 Pandas 的 DataFrame 添加另一列?dict 可以改变大小。
>>> import pandas as pd
>>> a = {'Test 1': 4, 'Test 2': 1, 'Test 3': 1, 'Test 4': 9}
>>> p = pd.DataFrame(a.items())
>>> p
0 1
0 Test 2 1
1 Test 3 1
2 Test 1 4
3 Test 4 9
[4 rows x 2 columns]
采纳答案by FooBar
If indeed percentage of 10
is what you want, the simplest way is to adjust your intake of the data slightly:
如果确实10
是您想要的百分比,最简单的方法是稍微调整您的数据摄入量:
>>> p = pd.DataFrame(a.items(), columns=['item', 'score'])
>>> p['perc'] = p['score']/10
>>> p
Out[370]:
item score perc
0 Test 2 1 0.1
1 Test 3 1 0.1
2 Test 1 4 0.4
3 Test 4 9 0.9
For real percentages, instead:
对于实际百分比,改为:
>>> p['perc']= p['score']/p['score'].sum()
>>> p
Out[427]:
item score perc
0 Test 2 1 0.066667
1 Test 3 1 0.066667
2 Test 1 4 0.266667
3 Test 4 9 0.600000
回答by joemar.ct
First, make the keys of your dictionary the index of you dataframe:
首先,将字典的键作为数据框的索引:
import pandas as pd
a = {'Test 1': 4, 'Test 2': 1, 'Test 3': 1, 'Test 4': 9}
p = pd.DataFrame([a])
p = p.T # transform
p.columns = ['score']
Then, compute the percentage and assign to a new column.
然后,计算百分比并分配给新列。
def compute_percentage(x):
pct = float(x/p['score'].sum()) * 100
return round(pct, 2)
p['percentage'] = p.apply(compute_percentage, axis=1)
This gives you:
这给你:
score percentage
Test 1 4 26.67
Test 2 1 6.67
Test 3 1 6.67
Test 4 9 60.00
[4 rows x 2 columns]