我有一个数据框df1:
Month
1
3
March
April
2
4
5
我还有另一个数据框df2:
Month Name
1 January
2 February
3 March
4 April
5 May
如果要用df2中的相应名称替换df1的整数值,可以使用哪种查找功能?
我想以此作为我的df1:
Month
January
March
March
April
February
May
答案 0 :(得分:5)
replace
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
答案 1 :(得分:3)
您可以先使用pd.Series.map
,然后再使用fillna
。请注意将字符串映射为字符串,或者将数字映射为数字:
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
您也可以使用pd.Series.replace
,但这是often inefficient。
答案 2 :(得分:1)
一种替代方法是将map与以下函数配合使用:
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)
输出
Month
0 January
1 February
2 March
3 April
4 May
答案 3 :(得分:1)
将map
与系列配合使用,只需确保您的dtypes匹配:
mapper = df2.set_index(df2['Month'].astype(str))['Name']
df1['Month'].map(mapper).fillna(df1['Month'])
输出:
0 January
1 March
2 March
3 April
4 February
5 April
6 May
Name: Month, dtype: object