简体   繁体   中英

Merge two dataframes by row/column in Pandas

I have two data-frames with identical columns:

df1 = pd.DataFrame({
    "A": ["B2", "B3", "B6", "B7"],
    "B": ["D2", "D3", "D6", "D7"],
    "C": ["F2", "F3", "F6", "F7"]})

df2 = pd.DataFrame({
        "A": ["A2", "A3", "A6", "A7"],
        "B": ["E2", "E3", "E6", "E7"],
        "C": ["Z2", "Z3", "Z6", "Z7"]})

I am trying to find a way to combine the two DFs in a new data-frame, wherein each cell combines the strings from the equivalent positions in df1 and df2 ; for example, column A at index position 0 would be the string "B2A2". The resulting data-frame would look something like this:

-----------------------------------
     A           B           C
-----------------------------------
  "B2A2"       "D2E2"      "F2Z2"
  "B3A3"       "D3E3"      "F3Z3"
  "B6A6"       "D6E6"      "F6Z6"
  "B7A7"       "D7E7"      "F7Z7"
-----------------------------------

I have tried looking at the Pandas documentation for merge and concat , but neither seem to do what I need.

Do you mean

df1 + df2

Output:

      A     B     C
0  B2A2  D2E2  F2Z2
1  B3A3  D3E3  F3Z3
2  B6A6  D6E6  F6Z6
3  B7A7  D7E7  F7Z7

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