我有以下字典:
rates = {'USD':
{'2019': 1,
'2020': 2,
'2021': 3},
'CAD':
{'2019': 4,
'2020': 5,
'2021': 6}
}我有以下虚拟数据:
Item Currency Year Rate
0 1 USD 2019
1 2 USD 2020
2 3 CAD 2021
3 4 CAD 2019
4 5 GBP 2020现在,我希望通过映射正确的速率( rate = f(currency,year) )来填充列rate = f(currency,year)。我试着:
def map_rate(data, rates):
for index, row in data.iterrows():
currency = str(row['Currency'])
if currency in list(rates.keys()):
year = str(row['Year'])
rate = rates[currency][year]
else:
rate = 1
return rate我使用上述内容如下:
df['Rate'] = map_rate(test, rates)然而,这只是返回第一个比率,例如值1,而不是适当的费率:
Item Currency Year Rate
0 1 USD 2019 1
1 2 USD 2020 1
2 3 CAD 2021 1
3 4 CAD 2019 1
4 5 GBP 2020 1预期结果是:
Item Currency Year Rate
0 1 USD 2019 1
1 2 USD 2020 2
2 3 CAD 2021 6
3 4 CAD 2019 4
4 5 GBP 2020 1我的错误在哪里?
发布于 2021-06-15 15:46:21
这里有一种方法,使用stack创建一个多索引系列,您可以使用df的值来reindex,以获得每行想要的费率。
df['rate'] = (
pd.DataFrame(rates)
.stack()
.reindex(pd.MultiIndex.from_frame(df[['Year','Currency']].astype(str)),
fill_value=1)
.to_numpy()
)
print(df)
Item Currency Year rate
0 1 USD 2019 1
1 2 USD 2020 2
2 3 CAD 2021 6
3 4 CAD 2019 4
4 5 GBP 2020 1发布于 2021-06-15 15:33:17
使用.apply
Ex:
df['Rate'] = df.apply(lambda x: rates[x['Currency']][x['Year']], axis=1)
# OR
df['Rate'] = df.apply(lambda x: rates.get(x['Currency'], dict()).get(x['Year'], 1), axis=1)
print(df)输出:
Item Currency Year Rate
0 1 USD 2019 1
1 2 USD 2020 2
2 3 CAD 2021 6
3 4 CAD 2019 4
4 5 GBP 2020 1发布于 2021-06-15 15:54:27
为费率创建另一个数据格式
rates_df = pd.DataFrame(rates).unstack().reset_index()
rates_df.columns = ['Currency', 'Year', 'Rates']
rates_df['Year'] = rates_df['Year'].astype(int)然后合并
df.merge(rates_df, on=['Currency', 'Year'], how='left').fillna(1)基准利率
Currency Year Rates
0 USD 2019 1
1 USD 2020 2
2 USD 2021 3
3 CAD 2019 4
4 CAD 2020 5
5 CAD 2021 6输出
Item Currency Year Rates
0 1 USD 2019 1.0
1 2 USD 2020 2.0
2 3 CAD 2021 6.0
3 4 CAD 2019 4.0
4 5 GBP 2020 1.0https://stackoverflow.com/questions/67989170
复制相似问题