我有这样阅读的数据框:
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
否则为{{1} }。像这样:
0
有没有快速的方法吗? 谢谢
答案 0 :(得分:4)
尝试np.select()
:
m1=df.eq(1).all(axis=1) #check if all column in each row is 1
m2=df.eq(0).all(axis=1) ##check if all column in each row is 0
使用np.select()
然后针对每个条件传递条件列表和选择列表(请参阅提供的链接中的文档)
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
答案 1 :(得分:1)