How To Create A Column In A Pandas Dataframe Based On A Conditional Substring Search Of One Or More OTHER Columns
I have the following data frame: import pandas as pd df = pd.DataFrame({'Manufacturer':['Allen Edmonds', 'Louis Vuitton 23', 'Louis Vuitton 8', 'Gulfstream', 'Bombardier', '23 - L
Solution 1:
Using .loc
to slice the dataframe, according to your conditions:
df.loc[(df['Manufacturer'].str.contains('Louis')) &
(df['System'].str.contains('Platinum')),
'Pricing'] = 'East Coast'
df
Manufacturer System Pricing
0 Allen Edmonds None NaN
1 Louis Vuitton 23 None NaN
2 Louis Vuitton 8 14 Platinum East Coast
3 Gulfstream Gold NaN
4 Bombardier None NaN
5 23 - Louis Vuitton Platinum 905 East Coast
6 Louis Vuitton 20 None NaN
Solution 2:
def contain(x):
if 'Louis' in x.Manufacturer and 'Platinum' in x.System:
return "East Coast"
df['pricing'] = df.apply(lambda x:contain(x),axis = 1)
Post a Comment for "How To Create A Column In A Pandas Dataframe Based On A Conditional Substring Search Of One Or More OTHER Columns"