简体   繁体   中英

For loop to merge Data frames

I am trying to merge data frames by looping since each loop merges the data frames based on a different column.

The following is what I have so far:

f1 = pd.DataFrame({"color": ["blue", "yellow", "red"],
                    "abbv": ["b", "y", "r"]})

df2 = pd.DataFrame({"color_1": ["blue", "red", "yellow"],
                        "color_2": ["yellow", "blue", "red"],
                        "total": ["green", "purple", "orange"]})

drop_column = df1.columns.tolist()
drop_column.remove("abbv") 

co = "color"
dd4 = []
for i in [1,2]:
    dd3 = pd.merge(df2,df1,
          left_on = f"{co}_{i}",
          right_on = "color",
          how="left")
    
    dd3 = dd3.rename(columns={"abbv":f"abbv_{i}"}).drop(drop_column, axis=1)
    
    dd4.append(dd3)

print(dd4)

This is the output:

          [  color_1 color_2   total abbv_1
          0    blue  yellow   green      b
          1     red    blue  purple      r
          2  yellow     red  orange      y,   color_1 color_2   total abbv_2
          0    blue  yellow   green      y
          1     red    blue  purple      b
          2  yellow     red  orange      r]

What I am trying to achieve:

color_1 color_2 total abbv_1 abbv_2
blue yellow green b y
. . . . .
. . . . .

If I understand your question right, you want to use .map :

m = df1.set_index("color")["abbv"]
df2["abbv_1"] = df2["color_1"].map(m)
df2["abbv_2"] = df2["color_2"].map(m)
print(df2)

Prints:

  color_1 color_2   total abbv_1 abbv_2
0    blue  yellow   green      b      y
1     red    blue  purple      r      b
2  yellow     red  orange      y      r

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