I have a dataframe "df":
Patient Condition
John Adductor tear left
Mary Adductor sprain left
Henry Hamstring sprain
Lucy Hamstring tear
You do not need to split the condition column. You can use the following code given the assumptions:
df
contains either "Adductor"
or "Hamstring"
in the Condition
column, case sensitive. "tear"
and "sprain"
.df["Muscle"] = df["Condition"].apply(lambda x: 1 if "Adductor" in x else 2)
Same thing with the Injury
column. You can try it and let me know if you need help.
If you do not want to worry about words being in upper or lower cases, you can use:
df["Muscle"] = df["Condition"].apply(lambda x: 1 if "adductor" in x.lower() else 2)
You could also use str.contains<\/code><\/a> to search for specific strings in Condition column and assign values using
np.select<\/code><\/a> .
import numpy as np
df['Muscle'] = np.select([df['Condition'].str.contains('Adductor'), df['Condition'].str.contains('Hamstring')], [1,2], np.nan)
df['Injury'] = np.select([df['Condition'].str.contains('tear'), df['Condition'].str.contains('sprain')], [1,2], np.nan)
Here is another way using enumerate()<\/code> and
assign()<\/code>
m = {j:i for i,j in enumerate(['adductor','hamstring'],1)}
i = {j:i for i,j in enumerate(['tear','sprain'],1)}
col = df['Condition'].str.split()
df.assign(Muscle = col.str[0].str.lower().map(m), Injury = col.str[1].str.lower().map(i))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.