Add a new column (conditional)
- apply
{% embed include file="src/examples/pandas/temprature.csv)
import sys
import pandas as pd
filename = 'temprature.csv'
df = pd.read_csv(filename)
print(df)
print()
def covid_instructions(row):
return "stay home" if row['temp'] >= 38 else "go to work"
df['covid_instructions'] = df.apply(covid_instructions, axis=1)
print(df)
Output:
name temp
0 Ella 36.6
1 Maor 40.0
2 Oren 38.2
3 Gal 37.0
name temp covid_instructions
0 Ella 36.6 go to work
1 Maor 40.0 stay home
2 Oren 38.2 stay home
3 Gal 37.0 go to work