简体   繁体   English

计算 Pandas dataframe 的每一列中满足条件的值的数量

[英]Count the number of values that satisfy a condition in every column of a Pandas dataframe

I have a dataframe with several columns of data.我有一个包含几列数据的 dataframe。 In the data, a -1 is equivalent to missing data.在数据中,-1 相当于缺失数据。 I want to count the number of -1 values in each column.我想计算每列中 -1 值的数量。

I believe I could register -1 as a NaN/missing value when I load the data and then I saw something that used isna() and counted boolean values.我相信我可以在加载数据时将 -1 注册为 NaN/缺失值,然后我看到一些使用 isna() 并计算 boolean 值的东西。 However, what I want to do (apply a condition to each column) seems like a fundamental thing I should know how to do, so I would like to figure out how to do it this way.但是,我想做的事情(对每一列应用条件)似乎是我应该知道如何做的基本事情,所以我想弄清楚如何做到这一点。

Here is an example.这是一个例子。 Imagine I have the following data frame:想象一下,我有以下数据框:

row   A   B  C  D  E
1     3   5  6  9 -1
2    -1   3 -1  2  0
3    -1  -1 -1  1 -1

The output I would like to get would be:我想得到的 output 是:

A  B  C  D  E
2  1  2  0  2

I have tried the following:我尝试了以下方法:

df.apply(lambda x: x == -1).count() # value returned was the count of all the rows
(df == -1).count() # also returned a count of all the rows.

I looked through several questions related to "countif", but they all seemed to apply a condition to one column to select rows.我查看了与“countif”相关的几个问题,但它们似乎都将条件应用于 select 行的一列。 And the two items I tried above were from questions related to apply functions to each column and count values that match a condition in each column.我在上面尝试的两项来自与将函数应用于每列和计算与每列中的条件匹配的值相关的问题。

The suggested duplicate in the comments is looking for a single value for the entire dataframe and different criteria on each column.评论中建议的重复项是为整个 dataframe 和每列的不同标准寻找单个值。 I am looking to apply the same condition to every column and get a result per column, as shown in the selected answer below.我希望将相同的条件应用于每一列并获得每列的结果,如下面的选定答案所示。

I would appreciate any thoughts or ideas on how to proceed.我将不胜感激有关如何进行的任何想法或想法。

Use DataFrame.eq + DataFrame.sum :使用DataFrame.eq + DataFrame.sum

#You can omit to_frame and T if you don't want a DataFrame.
df.eq(-1).sum().to_frame().T
#(df==-1).sum() #similar

or if it is str :或者如果是str

df.eq('-1').sum().to_frame().T

if row is a column:如果行是一列:

df[df.columns[1:]].eq(-1).sum().to_frame().T

   A  B  C  D  E
0  2  1  2  0  2

暂无
暂无

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

相关问题 删除满足列值条件的熊猫数据框的初始行,同时保持列中的序列值不变 - Delete initial rows of a pandas dataframe that satisfy column value condition while keeping the sequence values in a column intact 在大熊猫数据框中,算出某一列中某条件发生的次数? - In a pandas dataframe, count the number of times a condition occurs in one column? 计算熊猫数据框列中满足条件的单元格数 - Count number of cells satisfying a condition in pandas dataframe column 要检查 Pandas Dataframe 列是否为 TRUE/FALSE,如果为 TRUE,请检查另一列是否满足条件并生成具有值 PASS/FAIL 的新列 - To check Pandas Dataframe column for TRUE/FALSE, if TRUE check another column for condition to satisfy and generate new column with values PASS/FAIL 替换满足某些条件的 Pandas 列中的值会导致 SettingWithCopyWarning - Replace values in a pandas column that satisfy some condition leads to SettingWithCopyWarning 如果它们满足 Pandas 中的条件,如何将列的唯一值存储为列表? - how to store unique values of column as a list if they satisfy a condition in Pandas? 如果其他列中的值满足条件,则创建 pandas dataframe 列 - Create pandas dataframe columns if values in other column satisfy conditions 计算带有条件的熊猫数据框中出现的总数 - Count total number of occurrences in pandas dataframe with a condition Pandas DataFrame:在字符串列中查找唯一单词,根据条件计算它们在另一列中的出现和总和值 - Pandas DataFrame: Find unique words in string column, count their occurrence and sum values in another column on condition python count dataframe列值满足条件 - python count dataframe column values meeting condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM