python - Pandas inter-column referencing
问题描述
I have some data as follows:
+--------+------+
| Reason | Keys |
+--------+------+
| x | a |
| y | a |
| z | a |
| y | b |
| z | b |
| x | c |
| w | d |
| x | d |
| w | d |
+--------+------+
I want to get the Reason
corresponding to the first occurrence of each Key
. Like here, I should get Reasons
x,y,x,w
for Keys
a,b,c,d
respectively. After that, I want to compute the percentage of each Reason
, as in a metric for how many times each Reason
occurs. Thus x = 2/4 = 50%.
And w,y = 25%
each.
For the percentage, I think I can use something like value_counts(normalize=True) * 100
, based on the previous step. What is a good way to proceed?
解决方案
您对第二步是正确的,第一步可以通过
summary = df.groupby("Keys").first()
推荐阅读
- .net - 从 Web API 的 MongoDB 集合中获取所有文档
- vb.net - SqlConnectionStringBuilder:未设置 ApplicationName 属性
- bash - 如果更改了特定的配置,则执行管道
- visual-studio-code - 如何在 VSCode 和 Markdown Preview Enhanced 中显示 MathML?
- laravel - 如何从 laravel 中的集合中获取 ID?
- html - Jquery 将每个选定的 div 高度设置为等于其宽度
- python - 根据python中跨度的长度从元组值列表中删除重叠跨度
- function - 如何将字典传递给带有关键字参数的函数?
- java - JavaFX WebView 不呈现 - java.lang.NoSuchMethodError: 'boolean com.sun.prism.ResourceFactory.isDisposed()'
- javascript - 使用 Next js 时出现以下错误。我正在使用 axios 作为 http 客户端构建身份验证管道