Home > Mobile >  Set values in a column based on the values of other columns as a group
Set values in a column based on the values of other columns as a group

Time:11-07

I have a df that looks something like this:

  name A B C D
1 bar  1 0 1 1
2 foo  0 0 0 1
3 cat  1 0-1 0
4 pet  0 0 0 1
5 ser  0 0-1 0
6 chet 0 0 0 1

I need to use loc method to add values in a new column ('E') based on the values of the other columns as a group for instance if values are [1,0,0,0] value in column E will be 1. I've tried this:

d = {'A': 1, 'B': 0, 'C': 0, 'D': 0}
A = pd.Series(data=d, index=['A', 'B', 'C', 'D']) 
df.loc[df.iloc[:, 1:] == A, 'E'] = 1

It didn't work. I need to use loc method or other numpy based method since the dataset is huge. If it is possible to avoid creating a series to compare the row that would also be great, somehow extracting the values of columns A B C D and compare them as a group for each row.

Thanks

CodePudding user response:

You can compare values with A with test if match all rows in DataFrame.all:

df.loc[(df == A).all(axis=1), 'E'] = 1

For 0,1 column:

df['E'] = (df == A).all(axis=1).astype(int)

df['E'] = np.where(df == A).all(axis=1), 1, 0)
  • Related