I wanted to ask a question today, that how can I use the dataframe.str.contains function to search for a specific character in a if statement. I will elaborate the details in the following, and Thank you everyone for helping me.
So I want to do a calculation through the data, but first I need to sort the data by using the if function, the code that I wrote as following:
if df['ColumnA'].str.contains('-') is True:
df['Price']=df['ColumnB'].astype(float) df['ColumnC'].astype(float)/32
else:
df['Price']=df['ColumnB'].astype(float)/32
Basically, if there is a '-' contained in the Column A string, the price will be ColumnB ColumnC divided by 32, but if the column A does not contain the '-', the price will be ColumnB divided by 32
Does anyone know how can I settle the if statement? Thank you so much
CodePudding user response:
Your code checks the whole column for the character not element-by-element. You can use the numpy where
function (note there is a pandas where
function but it works in a slightly different way). So your code could be:
import numpy as np # better placed with other imports
df['Price'] = np.where(df['ColumnA'].str.contains('-'),
df['ColumnB'].astype(float) df['ColumnC'].astype(float)/32,
df['ColumnB'].astype(float)/32)