python - 如何获得一系列列表的基本统计数据?

标签 python python-2.7 pandas

我有一个 pandas DataFrame,我想获取它的基本统计信息,例如唯一值的数量、每个值出现的次数。类似于 df.describe .

我的问题是某些列有列表,我收到此错误:

>>> df["col_a"].nunique()
TypeError: unhashable type: 'list'

我的专栏如下所示:

col_a:
["a","b"]
["b","a"]
["c"]
["a","b","c"]
[]
NaN

处理此问题的最简单方法是什么?

最佳答案

转换为可散列的元组:

df['col_a'] = df['col_a'].dropna().apply(tuple)

输出:

       col_a
0     (a, b)
1     (b, a)
2       (c,)
3  (a, b, c)
4         ()
5        NaN

您现在可以执行此操作(返回5):

df['col_a'].nunique()

关于python - 如何获得一系列列表的基本统计数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41483330/

相关文章:

python - 如何将大字典的对象以特定比例分配给较小的字典?

python - 有没有带有文档字符串助手的 IDE?

Python Pandas 查找所有值为 NaN 的行

python - 无法导入 Pandas 和 numpy

python - Django PostgreSQL : migrating database to a different directory

python - 在 Django 中反向自引用外键

python - 必须指定IP地址吗?

Python 日志记录卡在特定字节 - 0x90

python - 地理 Pandas : sort a sample of points like a cycle graph

python - Pandas 将 Nan 列值更改为 True 或 False