简体   繁体   中英

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. Here is a bit of the lists:

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:

    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?

EDIT- Expected 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. 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']]])

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