应用于每列的pandas value_counts [英] pandas value_counts applied to each column

查看:143
本文介绍了应用于每列的pandas value_counts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dataframe,其中有来自外部源(csv文件)的许多列(≈30),但是其中有几列没有值或始终相同.因此,我将快速查看每一列的value_counts,我该怎么做?

I have a dataframe with numerous columns (≈30) from an external source (csv file) but several of them have no value or always the same. Thus, I would to see quickly the value_counts for each column, how can i do that?

例如

  Id, temp, name
1 34, null, mark
2 22, null, mark
3 34, null, mark

会给我一个说明的东西

  • 编号:34-> 2、22-> 1
  • temp:空-> 3
  • 名称:mark-> 3

所以我会知道temp是无关紧要的,并且名称并不有趣(总是一样)

So I would know that temp is irrelevant and name is not interesting (always the same)

推荐答案

对于数据框,

df = pd.DataFrame(data=[[34, 'null', 'mark'], [22, 'null', 'mark'], [34, 'null', 'mark']], columns=['id', 'temp', 'name'], index=[1, 2, 3]) 

以下代码

for c in df.columns:
    print "---- %s ---" % c
    print df[c].value_counts()

将产生以下结果:

---- id ---
34    2
22    1
dtype: int64
---- temp ---
null    3
dtype: int64
---- name ---
mark    3
dtype: int64

这篇关于应用于每列的pandas value_counts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆