简体   繁体   English

使用不同的列名连接 Pandas 中的两个数据框

[英]Joining Two Dataframes in Pandas With Different Column Names

I have two dataframes one displaying the transactions from a checking account and the other displaying transactions on a credit card.我有两个数据框,一个显示支票账户的交易,另一个显示信用卡的交易。 The only difference between the two lists is that the former seperates the transactions into 'debit' and 'credit' transactions and the latter only has the credit usage, a negative number indicating cashback and non-negative a normal purchase on the card.两个列表的唯一区别是前者将交易分为“借方”和“贷方”交易,后者只有贷方使用情况,负数表示现金返还,非负数表示卡上的正常购买。 I would like to 'join', if you will, these two while appending the transactions with negative numbers to the credit side of the first list and the non-negative ones to the debit side.如果您愿意,我想“加入”这两个,同时将带有负数的交易附加到第一个列表的贷方,将非负数附加到借方。 Besides those two columns there is a 'TransactionID' column that is has the same purpose for both lists.除了这两列之外,还有一个“TransactionID”列,它对两个列表具有相同的用途。 Here is a bit of the lists:这是一些列表:

df0: df0:

                                         TransactionID    Debit   Credit
0    HCCLAIMPMT BCBS TEXAS TRN*1*C20120E10592180*13...     0.00    21.29
1                    BANKCARD BTOT DEP 543052900022658     0.00   124.93
2                                          Check #1867  8755.50     0.00

df1: df1:

    Amount                    TransactionID
0    -3.41    YOUR CASH BACK THIS PERIOD IS
1    29.22              PAYPAL ON EBAY MARK
2    30.45                     REDTAGFABRIC

How do I go about joining them?我如何go加入他们?

EDIT- Expected output:编辑-预期 output:

                                      TransactionID       Debit   Credit
0    HCCLAIMPMT BCBS TEXAS TRN*1*C20120E10592180*13...     0.00    21.29
1                    BANKCARD BTOT DEP 543052900022658     0.00   124.93
2                                          Check #1867  8755.50     0.00
0                        YOUR CASH BACK THIS PERIOD IS     0.00     3.41
1                                  PAYPAL ON EBAY MARK    29.22     0.00      
2                                         REDTAGFABRIC    30.45     0.00      

It doesn't seem to me you want to join as in SQL join, but concat the two dataframes.在我看来你不想加入SQL 加入,而是连接两个数据帧。 To do so, notice that one of your debit or credit is zero.为此,请注意您的借方或贷方之一为零。 You can consider this:你可以考虑这个:

# assume df2 is the List2 dataframe, add "debit" and "credit" columns
df2['Debit'] = df2['Amount'].clip(0)
df2['Credit'] = (-df2['Amont']).clip(0)

# combine them
combined = pd.concat([df1, df2[['TransactionID', 'Debit', 'Credit']]])

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

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