简体   繁体   中英

How to create new column based off values from existing columns in pandas

I have a dataframe with 171 rows and 11 columns.

The 11 columns have values with either 0 or 1 how can i create a new column that will either be a 0 or 1, depending on whether the existing columns have a majority of 0 or 1?

you could do

(df.sum(axis=1)>df.shape[1]/2)+0

Use mean of rows and compare by DataFrame.gt for greater or DataFrame.ge for greater or equal 0.5 (it depends of output if same number of 0 and 1 ) and last convert mask to integers by Series.astype :

np.random.seed(20193)
df = pd.DataFrame(np.random.choice([0,1], size=(5, 4)))

df['new'] = df.mean(axis=1).gt(0.5).astype(int)
print (df)
   0  1  2  3  new
0  1  1  0  0    0
1  1  1  1  0    1
2  0  0  1  0    0
3  1  1  0  1    1
4  1  1  1  1    1

np.random.seed(20193)
df = pd.DataFrame(np.random.choice([0,1], size=(5, 4)))


df['new'] = df.mean(axis=1).ge(0.5).astype(int)
print (df)
   0  1  2  3  new
0  1  1  0  0    1
1  1  1  1  0    1
2  0  0  1  0    0
3  1  1  0  1    1
4  1  1  1  1    1
import numpy as np
import pandas as pd

X = np.asarray([(0, 0, 0),
                (0, 0, 1),
                (0, 1, 1),
                (1, 1, 1)])

df = pd.DataFrame(X)

df['majority'] = (df.mean(axis=1) > 0.5) + 0
df

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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