Pandas 如何从列表中计算真/假?
我试过:Pandas 如何从列表中计算真/假?,pandas,dataframe,count,Pandas,Dataframe,Count,我试过: len(df[df==True]) 掩蔽 它们在一个列表中,所以我不应该只计算它们吗?或者我是否需要为它们指定数值,1表示真,0表示假,然后使用计数或求和函数来确定有多少是真的?来计算列表中的真的 df = [bigdataframe[['Action', 'Adventure','Animation', 'Childrens', 'Comedy', 'Crime','Documentary',
len(df[df==True])
- 掩蔽
它们在一个列表中,所以我不应该只计算它们吗?或者我是否需要为它们指定数值,1表示真,0表示假,然后使用计数或求和函数来确定有多少是真的?来计算列表中的
真的
df = [bigdataframe[['Action', 'Adventure','Animation',
'Childrens', 'Comedy', 'Crime','Documentary',
'Drama', 'Fantasy', 'FilmNoir', 'Horror',
'Musical',
'Mystery', 'Romance','SciFi', 'Thriller', 'War',
'Western']].sum(axis=1) > 1]
df
Out[8]:
[0 True
1 True
2 True
3 True
4 True
5 False
6 True
7 True
8 False
9 True
10 False
11 True
12 True
13 True
14 True
15 False
16 True
17 False
18 True
19 False
20 False
21 True
22 True
23 True
24 False
25 True
26 True
27 True
28 True
29 True
99970 True
99971 True
99972 False
99973 True
99974 True
99975 True
99976 True
99977 True
99978 False
99979 False
99980 True
99981 False
99982 True
99983 False
99984 True
99985 True
99986 True
99987 True
99988 False
99989 True
99990 True
99991 True
99992 False
99993 True
99994 True
99995 True
99996 True
99997 True
99998 True
99999 False
Length: 100000, dtype: bool]
演示:
也许
df[[A','B','C']].sum(axis=1).gt(1.5).value_counts()
,:-)@Wen,谢谢!我以为OP只想计算真值
值,不是吗?那么为什么这不起作用呢。df=[bigdataframe[[动作片、冒险片、动画片、儿童片、喜剧片、犯罪片、纪录片、戏剧片、幻想片、黑色电影、恐怖片、音乐剧、神秘片、浪漫片、科幻片、惊悚片、战争片、西部片]]@Yungpythonnoob,我想这是因为您添加了方括号-[df[…].sum(axis=1)>1].sum()
将其替换为(df[…].sum(axis=1)>1.sum()
…@MaxUdf=(bigdataframe)([‘动作’、‘冒险’、‘动画’、‘儿童’、‘喜剧’、‘犯罪’、‘纪录片’、‘戏剧’、‘幻想’、‘黑色电影’、‘恐怖’、‘音乐剧’、‘神秘’、‘浪漫’、‘科幻’、‘惊悚片’、‘战争’、‘西部’)).sum(axis=1)>1.sum(),现在我得到错误:“DataFrame”对象不可调用。
sum(unlist(your.list.object))
In [386]: df = pd.DataFrame(np.random.rand(5,3), columns=list('ABC'))
In [387]: df
Out[387]:
A B C
0 0.228687 0.647431 0.526471
1 0.795122 0.915011 0.950481
2 0.386244 0.705412 0.420596
3 0.343213 0.928993 0.192527
4 0.201023 0.209281 0.304799
In [388]: df[['A','B','C']].sum(axis=1).gt(1.5)
Out[388]:
0 False
1 True
2 True
3 False
4 False
dtype: bool
In [389]: df[['A','B','C']].sum(axis=1).gt(1.5).sum()
Out[389]: 2