I have a data frame, want to create a column based on the string in column1_sport.
import pandas as pd
df = pd.read_csv('C:/Users/test/dataframe.csv', encoding = 'iso-8859-1')
Data contains:
column1_sport
baseball
basketball
tennis
boxing
golf
I want to look for certain strings ("ball" or "box") and create a new column based on whether the column contains that word. If the dataframe doesn't contain that word, add "other". See below.
column1_sport column2_type
baseball ball
basketball ball
tennis other
boxing box
golf other
For multiple conditions I suggest np.select. For example:
values = ['ball', 'box']
conditions = list(map(df['column1_sport'].str.contains, values))
df['column2_type'] = np.select(conditions, values, 'other')
print(df)
# column1_sport column2_type
# 0 baseball ball
# 1 basketball ball
# 2 tennis other
# 3 boxing box
# 4 golf other
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With