简体   繁体   English

应用groupby和size后,Python pandas访问True和False值

[英]Python pandas access to True and False values after groupby and size are applied

I have a dataframe with a column named diff. 我有一个名为diff的数据框。 I am able to group this column and get the number of True and False occurrences in the data frame. 我可以对该列进行分组,并获取数据框中出现的True和False的次数。

df.groupby('diff').size()

returns 退货

diff
True    5101
False     61
dtype: int64

I want to access to the value of True , 5101 . 我想访问True的值5101

I have already tried 我已经尝试过

df.groupby('diff').size().loc['True']

It is Series , so loc should be omit: 它是Series ,因此应省略loc

s = pd.Series([5101, 61], index=[True, False])
print (s)
True     5101
False      61
dtype: int64

print (s[True])
5101

The answer is: 答案是:

df_merged.groupby('diff').size().loc[True]

Explanation: note that 说明:注意

df_merged.groupby('diff').size().index

returns 退货

Index([True, False], dtype='object', name='diff')

It's a bool True , not a "True" like in a string !!!! 是布尔值True ,而不是字符串中的"True"

lambda使用.loc

s = df.groupby('diff').size().loc[lambda x :x]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM