Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 两个数据帧的复杂合并_Python_Pandas_Dataframe_Merge - Fatal编程技术网

Python 两个数据帧的复杂合并

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循环将该值作为列

我有两个数据帧:

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循环将该值作为列
[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]])