2

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.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Gus
  • 193
  • 9

1 Answers1

2

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)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • The problem is that I need to use .iloc since the df has other columns with other values. I fixed my question. Also if possible I just want to check the values directly without having to create a series to compare them to. – Gus Nov 05 '21 at 13:39
  • 1
    @Gus so use `df.loc[(df.iloc[:,1:]== A).all(axis=1), 'E'] = 1` – jezrael Nov 05 '21 at 14:44
  • jezrael Is there a way to replace "A" with something like [0,0,0,1] to avoid having to create a series? – Gus Nov 05 '21 at 17:58
  • 1
    @Gus Sure, here is possible use `A = [0,0,0,1]` – jezrael Nov 06 '21 at 05:19