Pandas Set Value If All Columns Are Equal In A Dataframe
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
Solution 1:
Try 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
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
Solution 2:
From the Pandas Documentation, I think the all() function will work for you. (Especially because your data seems to be in boolean form.)
df.all(axis=None)
This will evaluate the whole dataframe and return a True or False.
Post a Comment for "Pandas Set Value If All Columns Are Equal In A Dataframe"