Lax Mandis
Lax Mandis

Reputation: 131

Look Up in Python DataFrames?

I have a dataframe df1:

Month

1
3
March
April
2
4
5

I have another dataframe df2:

Month  Name

1       January
2       February
3       March
4       April
5       May

If I want to replace the integer values of df1 with the corresponding name from df2, what kind of lookup function can I use?

I want to end up with this as my df1:

    Month

January
March
March
April
February
May

Upvotes: 0

Views: 50

Answers (4)

Scott Boston
Scott Boston

Reputation: 153500

Use map with a series, just need to make sure your dtypes match:

mapper = df2.set_index(df2['Month'].astype(str))['Name']
df1['Month'].map(mapper).fillna(df1['Month'])

Output:

0     January
1       March
2       March
3       April
4    February
5       April
6         May
Name: Month, dtype: object

Upvotes: 1

jpp
jpp

Reputation: 164773

You can use pd.Series.map and then fillna. Just be careful to map either strings to strings or, as here, numeric to numeric:

month_name = df2.set_index('Month')['Name']

df1['Month'] = pd.to_numeric(df1['Month'], errors='coerce').map(month_name)\
                 .fillna(df1['Month'])

print(df1)

      Month
0   January
1     March
2     March
3     April
4  February
5     April
6       May

You can also use pd.Series.replace, but this is often inefficient.

Upvotes: 3

BENY
BENY

Reputation: 323326

replace it

df1.replace(dict(zip(df2.Month.astype(str),df2.Name)))
Out[76]: 
      Month
0   January
1     March
2     March
3     April
4  February
5     April
6       May

Upvotes: 5

Dani Mesejo
Dani Mesejo

Reputation: 61920

One alternative is to use map with a function:

def repl(x, lookup=dict(zip(df2.Month.astype(str), df2.Name))):
    return lookup.get(x, x)

df['Month'] = df['Month'].map(repl)
print(df)

Output

      Month
0   January
1  February
2     March
3     April
4       May

Upvotes: 1

Related Questions