[英]How to calculate growth in percentage between rows in a Pandas DataFrame?
[英]How to calculate between the rows in pandas Dataframe?
我想计算我的新值占原始值的百分比。 我想收到的是我的 Pandas DataFrame 中的新列。
我的 DataFrame 看起来像这样:
Feature Precision Accuracy Recall Specificity F1 score
0 original 0.949367 0.911765 0.9375 0.818182 0.943396
1 new_feat1 0.949367 0.911765 0.9375 0.818182 0.943396
2 new_feat2 0.950617 0.931373 0.9625 0.818182 0.956522
期望的结果如下所示:
Feature Precision Accuracy Recall Specificity F1 score Precision% Accuracy% Recall% Specificity% F1 score%
0 original 0.949367 0.911765 0.9375 0.818182 0.943396 100 100 100 100 100
1 new_feat1 0.949367 0.911765 0.9375 0.818182 0.943396 xxxxxx xxxxxx xxxxx xxxxxx xxxxxx
2 new_feat2 0.950617 0.931373 0.9625 0.818182 0.956522
所以计算应该是这样的:
对于 new_feat1:
或者,如果建议 Dataframe 可以采用这种格式更容易
original new_feat1 new_feat2
Precision 0.949367 0.950617 0.95122
Accuracy 0.911765 0.931373 0.941176
Recall 0.9375 0.9625 0.975
Specificity 0.818182 0.818182 0.818182
F1 score 0.943396 0.956522 0.962963
那么所需的 output 是:
original new_feat1 new_feat2
Precision 0.949367 0.950617 0.95122
Accuracy 0.911765 0.931373 0.941176
Recall 0.9375 0.9625 0.975
Specificity 0.818182 0.818182 0.818182
F1 score 0.943396 0.956522 0.962963
%Accuracy 100 xxxx xxxx
用div
和iloc[0]
除以第一行,用add_suffix
为列名添加后缀,然后用join
原始 DataFrame :
df.join(
df.select_dtypes(float).div(
df.select_dtypes(float).iloc[0]).add_suffix(' %'))
Output:
Feature Precision Accuracy Recall Specificity F1_score Precision % \
0 original 0.95 0.91 0.94 0.82 0.94 1.0
1 new_feat1 0.95 0.91 0.94 0.82 0.94 1.0
2 new_feat2 0.95 0.93 0.96 0.82 0.96 1.0
Accuracy % Recall % Specificity % F1_score %
0 1.00 1.00 1.0 1.00
1 1.00 1.00 1.0 1.00
2 1.02 1.03 1.0 1.01
最简单的方法可能是转置您的 dataframe:
df1_transposed = df1.T
然后,一旦你像往常一样做你的事情,如果你愿意,你可以调回来
df1 = df1_transposed.T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.