Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pandas dataframe:通过查找和计算现有数据帧来创建新的数据帧_Python_Pandas_Dataframe - Fatal编程技术网

Python pandas dataframe:通过查找和计算现有数据帧来创建新的数据帧

Python pandas dataframe:通过查找和计算现有数据帧来创建新的数据帧,python,pandas,dataframe,Python,Pandas,Dataframe,我试图使用两个日期框中的数据来创建一个新的数据框 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, 'attribu

我试图使用两个日期框中的数据来创建一个新的数据框

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
但我真的不知道如何从这两个数据帧中获取日期,并制作新的数据帧,或者使用:

df_lookup_data = pd.DataFrame(lookup_data,dtype=float)
items = [i['item'] for i in collected_data]
qnts = [i['qnt'] for i in collected_data]
print(df_lookup_data[df_lookup_data['item'].isin(items)].set_index('item').mul(qnts, 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
pear            0.0          0.0        210.0          0.0
或使用:

df_lookup_data = pd.DataFrame(lookup_data,dtype=float)
items = [i['item'] for i in collected_data]
qnts = [i['qnt'] for i in collected_data]
print(df_lookup_data[df_lookup_data['item'].isin(items)].set_index('item').mul(qnts, 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
pear            0.0          0.0        210.0          0.0

无需在此
合并
concat
。因为索引确实匹配,所以只需跨
axis=0

>>> df_lookup_data.mul(df_collected_data.qnt, axis=0)


无需在此
合并
concat
。因为索引确实匹配,所以只需跨
axis=0

>>> df_lookup_data.mul(df_collected_data.qnt, axis=0)


啊,我怎么没想到:比我的答案+1好多了!啊,我怎么没想到:比我的答案+1好多了!