Pandas Set Value If Most Columns Are Equal In A Dataframe
starting by another my question I've done yesterday Pandas set value if all columns are equal in a dataframe Starting by @anky_91 solution I'm working on something similar. Instead
Solution 1:
You could obtain the specified output from the following conditions:
thr = 0.7
c1 = (df.eq(1).sum(1)/df.shape[1]).gt(thr)
c2 = (df.eq(0).sum(1)/df.shape[1]).gt(thr)
c2.astype(int).mul(-1).add(c1)
Output
2009-08-02 02009-08-03 02009-08-04 02009-08-05 02009-08-06 -12009-08-07 1dtype:int64
Or using np.select
:
pd.DataFrame(np.select([c1,c2], [1,-1],0),index=df.index,columns=['result'])result2009-08-02 02009-08-03 02009-08-04 02009-08-05 02009-08-06 -12009-08-07 1
Solution 2:
Try with (m1
, m2
and tot
are same as what you have):
cond1=(m1>m2)&((m1 * 100/tot).gt(0.7))
cond2=(m2>m1)&((m2 * 100/tot).gt(0.7))
df['enseamble'] =np.select([cond1,cond2],[1,-1],0)
m =df.drop(df.columns.difference(['enseamble']), axis=1)
print(m)
enseamble
date
2009-08-02 1
2009-08-03 -1
2009-08-04 0
Post a Comment for "Pandas Set Value If Most Columns Are Equal In A Dataframe"