| 1st Most Common Value | 2nd Most Common Value | 3rd Most Common Value | 4th Most Common Value | 5th Most Common Value |
|-----------------------|-----------------------|-----------------------|-----------------------|-----------------------|
| Grocery Store | Pub | Coffee Shop | Clothing Store | Park |
| Pub | Grocery Store | Clothing Store | Park | Coffee Shop |
| Hotel | Theatre | Bookstore | Plaza | Park |
| Supermarket | Coffee Shop | Pub | Park | Cafe |
| Pub | Supermarket | Coffee Shop | Cafe | Park |
数据帧的名称是df0。正如您所看到的,在所有列中都有许多重复的值。所以我想创建一个数据帧,它包含所有列中的唯一值及其频率。有人能帮我写代码吗?因为我想画一个条形图
输出应如下所示:
| Venues | Count |
|----------------|-------|
| Bookstore | 1 |
| Cafe | 2 |
| Coffee Shop | 4 |
| Clothing Store | 2 |
| Grocery Store | 2 |
| Hotel | 1 |
| Park | 5 |
| Plaza | 1 |
| Pub | 4 |
| Supermarket | 2 |
| Theatre | 1 |
编辑:我在最初的回答中超越了自己(也感谢OP添加编辑/预期输出)。你想要this post,我认为最简单的答案是:
如果您不关心值来自哪个列,而只需要它们的计数,那么在this post之后使用
value_counts()
(正如@Celius Stingher在评论中所说的)如果确实要报告每列的每个值的频率,可以对每列使用
value_counts()
,但最终可能会出现不均匀的条目(要返回到DataFrame
,可以执行某种join
)相反,我创建了一个小函数来计算
df
中出现的值,并返回一个新值:这适用于填充了随机动物值名称的
df
。您只需通过获取其值的set
来传递df
中的唯一条目:df
之前:counted_entries()
的输出感谢您的编辑,也许这就是您想要的,使用
value_counts
作为完整的数据帧,然后聚合输出:输出:
您也可以这样做:
相关问题 更多 >
编程相关推荐