如果数据框中的所有列均相等,则 pandas 设置值 [英] Pandas set value if all columns are equal in a dataframe

查看:66
本文介绍了如果数据框中的所有列均相等,则 pandas 设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以这种方式读取的数据框:

I have this Dataframe that I read in this way:

df = pd.read_csv(myfile, delimiter=";")
df = df.set_index('date')
print(df)


      NET_0  NET_1  NET_2  NET_3  NET_4  NET_5  NET_6  NET_7  NET_8  NET_9  NET_10  NET_11  NET_12  NET_13  NET_14  NET_15  NET_16  NET_17  NET_18  NET_19  NET_20  NET_21  NET_22  NET_23  NET_24  NET_25
date                                                                                                                                                                                                            
2009-08-02      0      0      0      1      1      1      0      1      1      0       0       1       0       0       1       0       0       0       0       0       1       0       1       1       1       1
2009-08-03      0      0      0      1      1      1      0      0      1      0       1       1       0       0       1       1       0       0       0       0       1       0       1       1       1       1
2009-08-04      0      0      0      1      1      1      0      1      1      0       0       1       0       0       1       0       0       0       0       0       1       0       1       1       1       1
2009-08-05      0      0      0      1      1      1      0      1      1      0       1       1       0       0       1       0       0       0       0       0       1       0       1       1       1       1
2009-08-06      0      0      0      0      0      0      0      0      0      0       0       0      0      0      0      0      0      0      0      0      0      0      0      0      0      0      0      0      
2009-08-07      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1      1

我想要这样的结果:如果一行中的所有net_ *列都等于我想要的结果1,如果一行中的所有列均等于0 -1否则为0.像这样:

I want this results: if all net_* columns in a row are equals to one I want 1 as results, if all columns in a row are equals to 0 -1 else 0. Something like:

date    enseamble
2009-08-02     0
2009-08-03     0
2009-08-04     0
2009-08-05     0
2009-08-06     -1
2009-08-07     1

有没有快速的方法吗? 谢谢

There is a fast way without for? Thanks

推荐答案

尝试


使用np.select()然后针对每个条件传递条件列表和选择列表(请参阅提供的链接中的文档)


using np.select() to then pass the condition list and the choice list against each condition(refer docs in the link provided)

df['enseamble']=np.select([m1,m2],[1,-1],0) #using np.select expaination in docs
#to drop the remaining columns f, find difference between enseamble and other columns like below and call under axis=1:
m=df.drop(df.columns.difference(['enseamble']),axis=1) 
print(m)

                     enseamble
date                         0
2009-08-02 00:00:00          0
2009-08-03 00:00:00          0
2009-08-04 00:00:00          0
2009-08-05 00:00:00          0
2009-08-06 00:00:00         -1
2009-08-07 00:00:00          1

这篇关于如果数据框中的所有列均相等,则 pandas 设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆