pandas dataframe column based on another column 1

pandas dataframe column based on another column

df['c2'] = np.where(df.c1 == 8,'T', 'F')

   c1  c2  c3
0   4   F   1
1   8   T   9
2   1   F   8
3   3   F   5
4   3   F   8

Here is what the above code is Doing:
1. Create a new column called ‘c2’
2. np.where() is a vectorized if-else function, where a condition is checked for each cell in a column, and the output is assigned accordingly
3. The condition here is df.c1 == 8
4. The first argument is the condition
5. The second argument is what the new column should contain if the condition in #4 is true
6. The third argument is what the new column should contain if the condition in #4 is false

Similar Posts