简体   繁体   English

将pandas DataFrame与系列合并

[英]Merging a pandas DataFrame with a Series

I have this df: 我有这个df:

             cnpj
0  33062217000185
1  82645144000160

I run a function that creates two different Series: 我运行一个创建两个不同系列的函数:

for i in df.cnpj:
    s=peer_comparison(i)
    df=df.merge(peers.to_frame().T, how='left', on='cnpj')

In the first round of the for sentence, the output series goes like this: 在for语句的第一轮中,输出序列如下所示:

s (first round):

A                                  N/A
B                                  N/A
C                                  N/A
cnpj                    33062217000185

The merged dataframe looks like this: 合并的数据框如下所示:

             cnpj   A       B     C
0  33062217000185   N/A   N/A   N/A 
1  82645144000160   NaN   NaN   NaN 

When it goes to the second round of merging, the series look like this: 当进行第二轮合并时,该系列如下所示:

s (second round):

A                                  N/A
B                                  N/A
C                                  N/A
cnpj                    82645144000160

But the merging gets all messy, like this: 但是合并变得一团糟,就像这样:

             cnpj   A_x   B_x  C_x  A_y  B_y  C_y
0  33062217000185   N/A   N/A  N/A  NaN  NaN  NaN
1  82645144000160   NaN   NaN  NaN  N/A  N/A  N/A

If I try to change the merging using df.merge(s.to_frame().T.astype({'cnpj' : 'int'}), how='left',on='cnpj').fillna('') I get the following error: 如果我尝试使用df.merge(s.to_frame().T.astype({'cnpj' : 'int'}), how='left',on='cnpj').fillna('')更改合并df.merge(s.to_frame().T.astype({'cnpj' : 'int'}), how='left',on='cnpj').fillna('')出现以下错误:

ValueError: entry not a 2- or 3- tuple

Could anyone help? 有人可以帮忙吗?

Setup 设定

df = pd.DataFrame({'cnpj': [33062217000185, 82645144000160]})
print(df)
             cnpj
0  33062217000185
1  82645144000160

s = pd.Series(['N/A', 'N/A', 'N/A', 33062217000185], index=['A', 'B', 'C', 'cnpj'])
print(s)
A                  N/A
B                  N/A
C                  N/A
cnpj    33062217000185
dtype: object

Use df.merge , converting s to a dataframe and transposing in the process. 使用df.merge ,将s转换为数据帧并在过程df.merge置。

df.merge(s.to_frame().T\
      .astype({'cnpj' : 'int'}), how='left').fillna('')
             cnpj    A    B    C
0  33062217000185  N/A  N/A  N/A
1  82645144000160  

Getting some of @COLDSPEED tips and using concat instead of merge or join it finally worked. 获得一些@COLDSPEED技巧并使用concat而不是合并或加入它终于可以了。

peers=peer_comparison(df.cnpj[0])
for i in df.cnpj[1:]:
    peers2=peer_comparison(i,base_year)
    peers=pd.concat([peers,peers2],axis=1)

df=peers.T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM