gpt4 book ai didi

python - 在 Pandas 中,如何获取包含列表的 Series 的 value_counts()

转载 作者:太空宇宙 更新时间:2023-11-04 07:32:27 24 4
gpt4 key购买 nike

我有一个 pandas 系列 df.files,它看起来像这样:

In [79]: df.files
Out[79]:
0 [{'url': 'http://www.apkmirror.com/wp-content/...
1 [{'url': 'http://www.apkmirror.com/wp-content/...
2 [{'url': 'http://www.apkmirror.com/wp-content/...
3 [{'url': 'http://www.apkmirror.com/wp-content/...
4 [{'url': 'http://www.apkmirror.com/wp-content/...
5 [{'url': 'http://www.apkmirror.com/wp-content/...
6 [{'url': 'http://www.apkmirror.com/wp-content/...
7 [{'url': 'http://www.apkmirror.com/wp-content/...
8 [{'url': 'http://www.apkmirror.com/wp-content/...
9 [{'url': 'http://www.apkmirror.com/wp-content/...
10 [{'url': 'http://www.apkmirror.com/wp-content/...
11 [{'url': 'http://www.apkmirror.com/wp-content/...
12 [{'url': 'http://www.apkmirror.com/wp-content/...
13 [{'url': 'http://www.apkmirror.com/wp-content/...
14 [{'url': 'http://www.apkmirror.com/wp-content/...
15 [{'url': 'http://www.apkmirror.com/wp-content/...
16 [{'url': 'http://www.apkmirror.com/wp-content/...
17 [{'url': 'http://www.apkmirror.com/wp-content/...
18 [{'url': 'http://www.apkmirror.com/wp-content/...
19 [{'url': 'http://www.apkmirror.com/wp-content/...
20 [{'url': 'http://www.apkmirror.com/wp-content/...
21 [{'url': 'http://www.apkmirror.com/wp-content/...
22 [{'url': 'http://www.apkmirror.com/wp-content/...
23 [{'url': 'http://www.apkmirror.com/wp-content/...
24 [{'url': 'http://www.apkmirror.com/wp-content/...
25 [{'url': 'http://www.apkmirror.com/wp-content/...
26 [{'url': 'http://www.apkmirror.com/wp-content/...
27 [{'url': 'http://www.apkmirror.com/wp-content/...
28 [{'url': 'http://www.apkmirror.com/wp-content/...
29 [{'url': 'http://www.apkmirror.com/wp-content/...
...
16487 [{'url': 'http://www.apkmirror.com/wp-content/...
16488 []
16489 [{'url': 'http://www.apkmirror.com/wp-content/...
16490 [{'url': 'http://www.apkmirror.com/wp-content/...
16491 []
16492 [{'url': 'http://www.apkmirror.com/wp-content/...
16493 [{'url': 'http://www.apkmirror.com/wp-content/...
16494 [{'url': 'http://www.apkmirror.com/wp-content/...
16495 []
16496 []
16497 []
16498 [{'url': 'http://www.apkmirror.com/wp-content/...
16499 [{'url': 'http://www.apkmirror.com/wp-content/...
16500 [{'url': 'http://www.apkmirror.com/wp-content/...
16501 [{'url': 'http://www.apkmirror.com/wp-content/...
16502 [{'url': 'http://www.apkmirror.com/wp-content/...
16503 []
16504 []
16505 []
16506 []
16507 []
16508 []
16509 []
16510 []
16511 []
16512 []
16513 []
16514 []
16515 []
16516 []

一些值是空列表,而其他值是包含单个字典的列表,其格式类似于以下内容:

In [80]: df.files.loc[0]
Out[80]:
[{'checksum': '9f6075f4c561792e48354277b46a6810',
'path': 'full/80832b9fca82ce0f58f4d23c511e5a1d657c40e8.php?id=2968',
'url': 'http://www.apkmirror.com/wp-content/themes/APKMirror/download.php?id=2968'}]

我想知道有多少 df.files 的条目实际上是空列表。但是,如果我尝试 df.files.value_counts(),我会得到一个 TypeError: unhashable type: 'list'。我该如何解决这个问题?

最佳答案

如果想使用value_counts,可以先转换成tuple :

vc = df.files.apply(tuple).value_counts()

但如果只需要空列表长度,请使用str.len对于计数 lists,然后 sum bool 掩码的所有 True:

l = (df['files'].str.len() == 0).sum()

如果没有 NaN 的值是可能的,请使用 IanS solution :

l = (df['files'].apply(len) == 0).sum()

关于python - 在 Pandas 中,如何获取包含列表的 Series 的 value_counts(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44702584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com