Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 合并数据帧中的MUL操作_Python_Python 3.x_Pandas - Fatal编程技术网

Python 合并数据帧中的MUL操作

Python 合并数据帧中的MUL操作,python,python-3.x,pandas,Python,Python 3.x,Pandas,数据1 数据2 合并数据 df_invoices = pd.DataFrame([{'Customer' : 'Ali' , 'Product ID' : 4109 , 'Quantity' : 1}, {'Customer' : 'Eric' , 'Product ID' : 1412 , 'Quantity' : 12}, {'Customer' : 'Ande' , 'Product ID' :

数据1

数据2

合并数据

df_invoices = pd.DataFrame([{'Customer' : 'Ali' , 'Product ID' : 4109 , 'Quantity' : 1},
                        {'Customer' : 'Eric' , 'Product ID' : 1412 , 'Quantity' : 12},
                        {'Customer' : 'Ande' , 'Product ID' : 8931 , 'Quantity' : 6},
                        {'Customer' : 'Sam' , 'Product ID' : 4109 , 'Quantity' : 2}])

df_invoices = df_invoices.set_index('Product ID')
支付总金额的新数据帧

df_overall = pd.merge(df_products , df_invoices , how='outer' , left_index = 
True ,right_index = True)
我想显示所有带有结果的数据帧

但这最后一行行不通

df_total = df_overall['Price'] * df_overall['Quantity']

如果我理解正确,要在数据帧中定义新列,请使用:

    df_result = pd.merge(df_overall , df_total , how='outer' ,
left_index = True , right_index = True)
输出:

df_overall['Total'] = df_overall['Price'] * df_overall['Quantity']

print(df_overall)

可悲的是,我知道如何添加列,但当我将注意力集中在数据帧和数据上太久时,我会感到困惑。。非常感谢您的帮助,非常感谢。我看到您确实从coursera获得了数据科学专业。。实际上,我现在正在学习..我正试图在数据科学领域谋得一份职业,然后进入机器学习..你对此有什么看法?.从专业角度来看..我是一名应届毕业生,实际上我很喜欢UoM Coursera应用数据科学专业。它在Python和数据分析方面给了我很好的基础。
df_overall['Total'] = df_overall['Price'] * df_overall['Quantity']

print(df_overall)
            Price     Product Customer  Quantity  Total
Product ID                                             
1412          0.5         Egg     Eric        12    6.0
4109          5.0  Sushi Roll      Ali         1    5.0
4109          5.0  Sushi Roll      Sam         2   10.0
8931          1.5       Bagel     Ande         6    9.0