I want to add a new column that has a string value based on col1 and col2. So, if the value in col1 is greater than or equal to 4 and if col2 value is greater than or equal to 4 then add 'high' to col3 in the same row. Like in the image below.
Asked
Active
Viewed 439 times
-4
-
Is that Excel? pandas dataframe? Have you tried anything? There are many answers for that in SO, did you search for any? – Tomerikoo Mar 11 '21 at 15:54
-
Does this answer your question? [pandas create new column based on values from other columns / apply a function of multiple columns, row-wise](https://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns-apply-a-function-o) – Tomerikoo Mar 11 '21 at 15:55
3 Answers
1
Try with min() and comparison:
df['col3'] = np.where(df[['col1','col2']].min(1) >=4, 'High', 'Low')
Or since you have only two columns, you can compare directly:
df['col3'] = np.where(df['col1'].ge(4) & df['col2'].ge(4), 'High', 'Low')
Use lambda functions for that:
df['col3'] = df.apply(lambda row: 'High' if row['col1'] >=4 and row['col2'] >=4 else 'Low' ,axis=1)
output:
col1 col2 col3
0 1 4 Low
1 2 5 Low
2 3 6 Low
3 4 7 High
4 5 2 Low
Or in another way:
array = []
for item in df.values:
if item[0] >=4 and item[1] >=4: array.append('High')
else: array.append('Low')
df['col3'] = array

Amirhossein
- 92
- 8
1
Something like this should work, but it depends on the format of your data. I'm assuming it is a pandas DataFrame.
import numpy as np
df['col3'] = np.where((df['col1'] >= 4) & (df['col1'] >= 4), 'High', 'Low')

Leonardo Viotti
- 476
- 1
- 5
- 15
0
def test (row):
if row['col1'] >= 4 and row['col2'] >= 4:
return 'High'
else:
return 'Low'
df['col3'] = df.apply (lambda row: test (row), axis=1)
This is from the suggestion of @Tomerikoo, hope it is correct. But @Leonardo Viotti's answer is faster. Thanks! I have also learnt the np.where function now.

NoobTse
- 11
- 4