简体   繁体   中英

compare two different DataFrames

i'm newbie in Pandas, please help. I got two DataFrames to compare DF1:

Code Product Day C_Code Name Qty
001 Prod_X 01.01.2022 C_0001 Mr_X 12
002 Prod_Y 01.01.2022 C_0001 Mr_X 24
001 Prod_X 15.01.2022 C_0002 Mr_X 12
002 Prod_Y 16.01.2022 C_0003 Mr_X 24
001 Prod_X 17.01.2022 C_0004 Mr_X 12

DF2:

Day C_Code Name Qty
01.01.2022 C_0001 Mr_X 36
15.01.2022 C_0002 Mr_X 12
16.01.2022 C_0003 Mr_X 12

Column Day & Column C_Code unique.Conditions if DF1[ Day ]&[ C_Code ] == DF2 [ Day ]&[ C_Code ] then compare QTY , if DF1.sum[ QTY ]<= DF2[ QTY ] write row into DF3, if DF1[ QTY ] > DF2[ QTY ] then row from DF1 write into DF3 and put value from DF2[ QTY ], difference between DF1[ QTY ] and DF2[ QTY ] write into DF4. in the output should be

Correct List:

Code Product Day C_Code Name Qty
001 Prod_X 01.01.2022 C_0001 Mr_X 12
002 Prod_Y 01.01.2022 C_0001 Mr_X 24
001 Prod_X 15.01.2022 C_0002 Mr_X 12
002 Prod_Y 16.01.2022 C_0003 Mr_X 12

Incorrect List:

Code Product Day C_Code Name Qty
002 Prod_Y 16.01.2022 C_0003 Mr_X 12
001 Prod_X 17.01.2022 C_0004 Mr_X 12

How i understand i need to use groupBy, but i don't understand how a lot of tnx for help!

import pandas as pd 你试过了吗?

df = pd.merge(DF1, DF2, how='left', on='Day', indicator=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM