dataframe按列求百分比
时间: 2023-11-18 18:13:53 浏览: 309
Pandas实现DataFrame按行求百分数(比例数)
要按列求百分比,可以使用Pandas库中的`div`方法和`sum`方法来实现。其中,`div`方法用于将每个元素除以该列的和,`sum`方法用于计算每列的总和。具体代码如下所示:
```python
import pandas as pd
# 创建一个示例DataFrame
df = pd.DataFrame({'A':[1, 2, 3], 'B':[4, 5, 6], 'C':[7, 8, 9})
# 按列求百分比
result = df.div(df.sum())
# 输出结果
print(result)
```
这样,就可以得到按列求百分比后的DataFrame。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [python pandas 求DataFrame中各数值的百分比](https://blog.csdn.net/small__roc/article/details/124611366)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [R语言使用quantile函数计算dataframe中特定数据列的百分位数、通过设置probs参数计算百分位数](https://blog.csdn.net/weixin_57242009/article/details/123193251)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [undefined](undefined)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文