Python 两个数据帧的复杂合并
我有两个数据帧: df1: df2: 如何合并或迭代两个数据帧以获得以下结果:Python 两个数据帧的复杂合并,python,pandas,dataframe,merge,Python,Pandas,Dataframe,Merge,我有两个数据帧: df1: df2: 如何合并或迭代两个数据帧以获得以下结果: top1 top2 top3 693541495124446625 'US 939.00' 'GB 932.00' 'CN 806.00' 912819499544441670 'US 992.00' 'CN 981.00' 'TW 796.00' 我知道我可以迭代获取df1值,并通过几个for循环将该值作为列
top1 top2 top3
693541495124446625 'US 939.00' 'GB 932.00' 'CN 806.00'
912819499544441670 'US 992.00' 'CN 981.00' 'TW 796.00'
我知道我可以迭代获取df1值,并通过几个for循环将该值作为列
[loc]
放在df2中,但是否有任何优化的解决方案?您可以使用df尝试此操作。替换
u = df2.astype(str).radd(df2.columns+' ')
out = df1.T.replace(u.T).T
或:
差不多
out = df1.T.replace(df2.T).astype('str').radd(df1.T+' ').T
Out[317]:
top1 top2 top3
693541495124446625 US 939.0 GB 932.0 CN 806.0
912819499544441670 US 992.0 CN 981.0 TW 796.0
首先,将第二个数据帧转换为字典字典:
df2_dict = {i:None for i in df2.index}
for key in df2_dict:
df2_dict[key] = {col: df2.loc[key, col] for col in df2.columns}
然后,您可以使用与ds1相同的行和列创建一个新的df,并对其进行迭代:
df3 = pd.DataFrame(index=df1.index,
columns=df1.columns)
for i in df3.index:
for col in df3.columns:
df3.loc[i, col] = df1.loc[i, col] + ' ' + str(df2_dict[i][df1.loc[i, col]])
那是个好主意~
print(out)
top1 top2 top3
693541495124446625 US 939.0 GB 932.0 CN 806.0
912819499544441670 US 992.0 CN 981.0 TW 796.0
out = df1.T.replace(df2.T).astype('str').radd(df1.T+' ').T
Out[317]:
top1 top2 top3
693541495124446625 US 939.0 GB 932.0 CN 806.0
912819499544441670 US 992.0 CN 981.0 TW 796.0
df2_dict = {i:None for i in df2.index}
for key in df2_dict:
df2_dict[key] = {col: df2.loc[key, col] for col in df2.columns}
df3 = pd.DataFrame(index=df1.index,
columns=df1.columns)
for i in df3.index:
for col in df3.columns:
df3.loc[i, col] = df1.loc[i, col] + ' ' + str(df2_dict[i][df1.loc[i, col]])