简体   繁体   English

根据所有其他列的条件计数创建新的 Pandas 列

[英]Create new Pandas columns based on conditional counts of all other columns

I have a Pandas data frame with around ~360 columns.我有一个大约 360 列的 Pandas 数据框。 I want to add another column to the other frame based on the count of whether in rest of the columns how many columns have a value greater than 0. Type of all my columns is float 64.我想根据列的 rest 中是否有多少列的值大于 0 的计数向另一个框架添加另一列。我所有列的类型都是 float 64。

If this is my original dataframe如果这是我原来的 dataframe

column A column B column C ...............Column Z 
0          1.5      6.77                   3.33
3.5        4.5      0                       0  
0           0       0.98                    0
.
.
.
2.35      4.32       0                      9.21 

I want to add a column to it like this which have count of all non zero columns for each row:我想像这样向它添加一个列,其中包含每行的所有非零列的计数:

column A column B column C ...............Column Z  Column New
0          1.5      6.77                   3.33        3
3.5        4.5      0                       0          2
0           0       0.98                    0          1
.                                                      .
.                                                      .
.                                                      .
2.35      4.32       0                      9.21       3 

How can i add a column like this?我怎样才能添加这样的列? I was trying this but getting all NaNs.我正在尝试这个,但得到了所有的 NaN。

df['column_new']=df.apply(lambda x: (x > 0).count())

Not sure if this is the correct way approach this.不确定这是否是解决此问题的正确方法。

Just to give an idea this is how values in my original data frame are like.只是为了给出一个想法,这就是我原始数据框中的值的样子。 So i want a count of columns in the new column which don`t have zero value.所以我想要一个新列中不为零值的列的计数。

在此处输入图像描述

You're on the right track.你在正确的轨道上。 The comparison to zero returns series of bools, so you can sum rather than count.与零的比较返回一系列布尔值,因此您可以求和而不是计数。 You also don't need to apply this since you can compare the entire dataframe and then sum along the correct axis:您也不需要apply它,因为您可以比较整个 dataframe 然后沿正确的轴求和:

df = df.assign(count_gt_zero=(df > 0).sum(axis='columns'))

Another option with gt : gt的另一个选项:

df['new_column'] = df.gt(0, axis=1).sum(axis=1)

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

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