Ad

Python: Populate New Df Column Based On If Statement Condition

- 1 answer

I'm trying something new. I want to populate a new df column based on some conditions affecting another column with values.

I have a data frame with two columns (ID,Retailer). I want to populate the Retailer column based on the ids in the ID column. I know how to do this in SQL, using a CASE statement, but how can I go about it in python?

I've had look at this example but it isn't exactly what I'm looking for.

Python : populate a new column with an if/else statement

import pandas as pd

data = {'ID':['112','5898','32','9985','23','577','17','200','156']}

df = pd.DataFrame(data)

df['Retailer']=''

if df['ID'] in (112,32):
    df['Retailer']='Webmania'
elif df['ID'] in (5898):
    df['Retailer']='DataHub'
elif df['ID'] in (9985):
    df['Retailer']='TorrentJunkie'
elif df['ID'] in (23):
    df['Retailer']='Apptronix'
else: df['Retailer']='Other'


print(df)

The output I'm expecting to see would be something along these lines:

     ID Retailer
0   112 Webmania
1  5898 DataHub
2    32 Webmania
3  9985 TorrentJunkie
4    23 Apptronix
5   577 Other
6    17 Other
7   200 Other
8   156 Other

Ad

Answer

Use numpy.select and for test multiple values use Series.isin, also if need test strings like in sample data change numbers to numeric like 112 to '112':

m1 = df['ID'].isin(['112','32'])
m2 =  df['ID'] == '5898'
m3 =  df['ID'] == '9985'
m4 =  df['ID'] == '23'
vals = ['Webmania', 'DataHub', 'TorrentJunkie', 'Apptronix']
masks = [m1, m2, m3, m4]

df['Retailer'] = np.select(masks, vals, default='Other')
print(df)

     ID       Retailer
0   112       Webmania
1  5898        DataHub
2    32       Webmania
3  9985  TorrentJunkie
4    23      Apptronix
5   577          Other
6    17          Other
7   200          Other
8   156          Other

If many catagories also is possible use your solution with custom function:

def get_data(x):
    if x in ('112','32'):
        return 'Webmania'
    elif x == '5898':
        return 'DataHub'
    elif x == '9985':
        return 'TorrentJunkie'
    elif x == '23':
        return 'Apptronix'
    else: return 'Other'


df['Retailer'] =  df['ID'].apply(get_data)
print (df)
     ID       Retailer
0   112       Webmania
1  5898        DataHub
2    32       Webmania
3  9985  TorrentJunkie
4    23      Apptronix
5   577          Other
6    17          Other
7   200          Other
8   156          Other

Or use map by dictionary, if no match get NaN, so added fillna:

d = {'112': 'Webmania','32':'Webmania',
    '5898':'DataHub',
    '9985':'TorrentJunkie',
    '23':'Apptronix'}

df['Retailer'] =  df['ID'].map(d).fillna('Other')
Ad
source: stackoverflow.com
Ad