我有两个数据:
df1 = pd.DataFrame(index = [0,1,2], columns=['USD', 'CAD'])
df1['USD'] = [1,2,3]
df1['CAD'] = [4,5,6]
df1:
USD CAD
0 1 4
1 2 5
2 3 6
df2 = pd.DataFrame(index = [0,1], columns = ['currency','balance'])
df2['currency'] = ['USD', 'CAD']
df2['balance'] = [2,3]
df2:
currency balance
0 USD 2
1 CAD 3我想在索引0处向df1添加一行,并根据货币填充该行中的df2余额。因此,最终的df应该如下所示:
df:
USD CAD
0 2 3
1 1 4
2 2 5
3 3 6我怎么能用节奏曲的方式做这件事?谢谢
发布于 2022-02-13 04:44:55
尝试以下代码:
df = pd.concat([df2.set_index('currency').T, df1], axis=0, ignore_index=True)https://stackoverflow.com/questions/71097945
复制相似问题