我有一个列,其中包含各种大小的列表,但项目数量有限。
print(df['channels'].value_counts(), '\n')
输出:
[web, email, mobile, social] 77733
[web, email, mobile] 43730
[email, mobile, social] 32367
[web, email] 13751
所以我想要的是网络、电子邮件、移动和社交的总次数。
这些应该是:
web = 77733 + 43730 + 13751 135,214
email = 77733 + 43730 + 13751 + 32367 167,581
mobile = 77733 + 43730 + 32367 153,830
social = 77733 + 32367 110,100
我尝试了以下两种方法:
sum_channels_items = pd.Series([x for item in df['channels'] for x in item]).value_counts()
print(sum_channels_items)
from itertools import chain
test = pd.Series(list(chain.from_iterable(df['channels']))).value_counts()
print(test)
这两种方法都会失败,并出现相同的错误(仅显示第二个错误)。
Traceback (most recent call last):
File "C:/Users/Mark/PycharmProjects/main/main.py", line 416, in <module>
test = pd.Series(list(chain.from_iterable(df['channels']))).value_counts()
TypeError: 'float' object is not iterable