简体   繁体   English

如何合并两个数据框熊猫

[英]How to merge two dataframes panda

I have two data frames that I am trying to join yet not successfully.我有两个数据框,我试图加入但没有成功。 Here is the data frame:这是数据框:

 df1
     symbol count
 0.  AAL      20
 1.  BBY      34
 2.  CLL      23
 3.  DKG      12
 4.  LMND     20

 df2
     symbol TYPE count
 0.  AAL    CALL  11
 1.  AAL    PUT   9
 2.  BBY    CALL  30
 3.  BBY    PUT   4
 4.  CLL    CALL  23
 5.  CLL    PUT   3
 6.  DKG    CALL  6
 7.  DKG    PUT   6
 8.  LMND   CALL  10
 9.  LMND   PUT   10

Expected out put

 symbol  TotalCount  Put  Call
 0.  AAL    20         9    11
 1.  BBY    34         4    30 
 2.  CLL    26         3    23
 3.  DKG    12         6    6
 4.  LMND   20         10   10

Here is my attempt: newdf = pd.merge(df1,df2, how='left') yet this is not marging it right.这是我的尝试: newdf = pd.merge(df1,df2, how='left')但这并没有保证正确。 What am I missing?我错过了什么?

You need to create the Call and Put columns first (which can be achieved by pivoting) before merging:在合并之前,您需要先创建CallPut列(这可以通过旋转来实现):

(
    df1.merge(df2.pivot("symbol", "TYPE", "count"), on="symbol", how="left")
    .assign(TotalCount=lambda x: x['CALL'] + x['PUT'])
    .drop(columns="count")
)

  symbol  CALL  PUT  TotalCount
0    AAL    11    9          20
1    BBY    30    4          34
2    CLL    23    3          26
3    DKG     6    6          12
4   LMND    10   10          20

You can use a combination of groupby and unstack :您可以结合使用groupbyunstack

df1.merge(
    df2.groupby(['symbol', 'TYPE'])['count'].max().unstack('TYPE'),
    how='left', left_on='symbol', right_on='symbol'
)

  symbol  count  CALL  PUT
0    AAL     20    11    9
1    BBY     34    30    4
2    CLL     23    23    3
3    DKG     12     6    6
4   LMND     20    10   10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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