Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create New Column Based On String

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
like image 260
nia4life Avatar asked Nov 19 '25 08:11

nia4life


1 Answers

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
like image 200
jpp Avatar answered Nov 20 '25 20:11

jpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!