我试图通过循环来合并数据帧,因为每个循环都基于不同的列合并数据帧。
以下是我目前所拥有的:
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)
这是输出:
[ 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]
我想要实现的目标:
color_1 | color_2 | 总计 | abbv_1 | abbv_2 |
---|---|---|---|---|
蓝色 | 黄色 | 绿色 | b | y |
. | . | . | . | . |
. | . | . | . | . |
答案 0 :(得分:1)
如果我理解你的问题,你想使用public class PaymentModel
{
public string CreditorName { get; set; } = default!;
public CreditorAccounts CreditorAccount { get; set; } = default!;
public DebtorAccounts DebtorAccount { get; set; } = default!;
public InstructedAmounts InstructedAmount { get; set; } = default!;
public string RemittanceInformationUnstructured { get; set; } = default!;
public class CreditorAccounts
{
public string Iban { get; set; } = default!;
public string Currency { get; set; } = default!;
}
public class DebtorAccounts
{
public string Iban { get; set; } = default!;
public string Currency { get; set; } = default!;
}
public class InstructedAmounts
{
public string Currency { get; set; } = default!;
public string Amount { get; set; } = default!;
}
public class PaymentAccounts
{
public string Iban { get; set; } = default!;
public string Currency { get; set; } = default!;
}
public class PaymentAmounts
{
public string Currency { get; set; } = default!;
public string Amount { get; set; } = default!;
}
}
:
.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)