Shi Jie Tio
Shi Jie Tio

Reputation: 2529

How to apply wildcard in python pandas dataframe?

I have one dataframe:

Car       Color 
BMW       WHITE
BMPrO     Black
Toyota    Green
Honda     Black

I wish to create a column which based on the Car column, if Car column start with BM* then ID column will be A IF Car column startwith T then ID column will be B else C

Expected dataframe

Car       Color   ID
BMW       WHITE   A
BMPrO     Black   A
Toyota    Green   B
Honda     Black   C

Anyone have ideas?

I have try this code but not work for this case:

def f(row):
    if row['car']=='B*':
        val = A
    elif row['Car'] =='T*':
        val = B
    else:
        val = 'C'
    return val
df['ID'] = df.apply(f, axis=1)

Upvotes: 2

Views: 1154

Answers (3)

Shijith
Shijith

Reputation: 4882

you can create a list and add as 'ID' Column to the data frame

df = pd.DataFrame({"Car": ['BMW','BMPro','Toyota','Honda'],"Color" : ['White','Black','Green','Black']})

df['ID'] = ['A' if x.startswith('BM') else 'B' if x.startswith('T') else 'C' for x in df['Car']]

    Car     Color   ID
0   BMW     White   A
1   BMPro   Black   A
2   Toyota  Green   B
3   Honda   Black   C

Upvotes: 0

run-out
run-out

Reputation: 3184

Create a third column and make it 'C' for all else.

df['ID'] = 'C'

Set the value of 'ID' based on criteria for words starting with BM and T

df.loc[df['Car'].str.startswith('BM'), 'ID'] = 'A'
df.loc[df['Car'].str.startswith('T'), 'ID'] = 'B'

       Car Color ID
0     BMW  WHITE  A
1   BMPrO  Black  A
2  Toyota  Green  B
3   Honda  Black  C

Upvotes: 0

anky
anky

Reputation: 75100

Try creating 2 conditions using series.str.startswith() and df.apply() and use np.select() to put the condition against choices:

m=df.apply(lambda x: x.str.startswith('BM')).any(axis=1)
n=df.apply(lambda x: x.str.startswith('T')).any(axis=1)

df['ID']=np.select([m,n],['A','B'],'C')
print(df)

      Car  Color ID
0     BMW  WHITE  A
1   BMPrO  Black  A
2  Toyota  Green  B
3   Honda  Black  C

Upvotes: 1

Related Questions