[英]Look Up in Python DataFrames?
I have a dataframe df1: 我有一个数据框df1:
Month
1
3
March
April
2
4
5
I have another dataframe df2: 我有另一个数据框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? 如果要用df2中的相应名称替换df1的整数值,可以使用哪种查找功能?
I want to end up with this as my df1: 我想以此作为我的df1:
Month
January
March
March
April
February
May
replace
it 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
You can use pd.Series.map
and then fillna
. 您可以使用
pd.Series.map
然后使用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 . 您也可以使用
pd.Series.replace
,但这通常效率不高 。
Use map
with a series, just need to make sure your dtypes match: 将
map
与一系列序列配合使用,只需确保您的dtypes匹配:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.