Home > database >  Fill the last column in dataframe with condition
Fill the last column in dataframe with condition

Time:10-18

I have a dataframe like this:

   col0  col1  col2  col3  col4
0     0     0     0     1     0
1     0     0     1     0     0
2     0     0     0     0     0
3     0     0     0     0     0

How can I make a new one with the same entries but the last column to be 1 if the one doesn't appear earlier? It should look like this:

   col0  col1  col2  col3  col4
0     0     0     0     1     0
1     0     0     1     0     0
2     0     0     0     0     1
3     0     0     0     0     1

CodePudding user response:

Let's try to compare the earlier columns with 1 and check if 1 exists in each row

condition.iloc[condition.iloc[:, :-1].eq(1).sum(axis=1).eq(0), -1] = 1
# or
condition.iloc[~condition.iloc[:, :-1].eq(1).any(axis=1), -1] = 1
print(condition)

   0  1  2  3  4
0  0  0  0  1  0
1  0  0  1  0  0
2  0  0  0  0  1
3  0  0  0  0  1

CodePudding user response:

how about this without the indexing:

condition.drop(condition.columns[len(condition.columns)-1],axis=1,inplace=True)
print(pd.concat([condition,(~condition.any(1)).astype(int)],axis=1).T.reset_index(drop=True).T)


   0  1  2  3  4
0  0  0  0  1  0
1  0  0  1  0  0
2  0  0  0  0  1
3  0  0  0  0  1

CodePudding user response:

Code

# Make a New dataframe with the same value
condition_2 = condition.copy()

# For each row, change last column to 1 if no 1 in previous columns
condition_2.iloc[:, -1] = np.where(~(condition.iloc[:, :-1]==1).any(axis = 1), 1, condition_2.iloc[:, -1])

Explanation

condition_2.iloc[:, -1]   # last column of dataframe condition_2
~(condition.iloc[:, :-1]==1).any(axis = 1) mask which is True if not a 1 in previous column of each row
np.where(...)             # allows for obtaining indexes where mask condition is  satisfied

CodePudding user response:

You can use the mathod any:

df['col4'] = (~df.any(axis=1)).astype(int)

If you need to ignore the last column:

df['col4'] = (~df.drop('col4', axis=1).any(axis=1)).astype(int)
  • Related