我正在尝试使用来自两个日期帧的数据来创建一个新的数据帧
lookup_data = [
{ 'item': 'apple',
'attribute_1':3,
'attribute_2':2,
'attribute_3':10,
'attribute_4':0,
},
{ 'item': 'orange',
'attribute_1':0.4,
'attribute_2':20,
'attribute_3':1,
'attribute_4':9,
},
{ 'item': 'pear',
'attribute_1':0,
'attribute_2':0,
'attribute_3':30,
'attribute_4':0,
},
{ 'item': 'peach',
'attribute_1':2,
'attribute_2':2,
'attribute_3':3,
'attribute_4':6,
},]
df_lookup_data = pd.DataFrame(lookup_data,dtype=float)
df_lookup_data.set_index('item', inplace=True, drop=True)
collected_data = [
{ 'item':'apple',
'qnt': 4},
{ 'item':'orange',
'qnt': 2},
{ 'item':'pear',
'qnt': 7},
]
df_collected_data = pd.DataFrame(collected_data,dtype=float)
df_collected_data.set_index('item', inplace=True, drop=True)
df_result = pd.DataFrame(
.... first column is item type
.... second column is qnt*attribute_1
.... second column is qnt*attribute_2
.... second column is qnt*attribute_3
.... second column is qnt*attribute_4
)
df_result.columns = ['item', 'attribute_1', 'attribute_2', 'attribute_3', 'attribute_4']
print(result)
应该打印结果
item attribute_1 attribute_2 attribute_3 attribute_4
0 apple 14 8 40 0
1 orange 0.8 40 2 18
2 pear 0 0 210 0
但我真的不确定如何从这两个数据帧中获取日期并制作这个新数据帧
请您参考如下方法:
这里不需要merge 或concat。由于索引 确实 匹配,因此只需在 axis=0
mul
>>> df_lookup_data.mul(df_collected_data.qnt, axis=0)
attribute_1 attribute_2 attribute_3 attribute_4
item
apple 12.0 8.0 40.0 0.0
orange 0.8 40.0 2.0 18.0
peach NaN NaN NaN NaN
pear 0.0 0.0 210.0 0.0
