简体   繁体   English

有什么方法可以组合 2 个 pandas 数据帧?

[英]Is there any method to combine 2 pandas dataframes?

I want to combine 2 dataframe.我想组合 2 个 dataframe。 However, it has one problem.但是,它有一个问题。 How do I combine 2 table if 1 table value have to duplicate to another table.如果 1 个表值必须复制到另一个表,我如何合并 2 个表。

I have tried, pandas.concat and pandas.merge.我试过了,pandas.concat 和 pandas.merge。

df1={'id':[1]}
df2={'Brand':['volvo','audi'],
     'Price':[20,000,30,000]}

pd.concat([df1],[df2])

I expect the table will show as below:我希望表格如下所示:

id  Brand   Price
1   volvo   20,000
1   audi    30,000

It mean the id 1 has both car.这意味着 id 1 有两辆车。

You have to decide how you want to merge, if there are multiple values.如果有多个值,您必须决定如何合并。 If there is just 1, then you can simply assign like:如果只有 1,那么您可以简单地分配如下:

df1['key'] = 1
df2['key'] = 1

Perform a merge on the temp key, then drop the temporary key:对临时键执行合并,然后删除临时键:

df1.merge(df2).drop(columns=['key'])

Output: Output:

 id  Brand  Price
 0   1  volvo  20000
 1   1   audi  30000

But you are performing a Cartesian product, so if there are multiple value in df1, eg: [1,2] , you will have more duplicate data:但是您正在执行笛卡尔积,因此如果 df1 中有多个值, eg: [1,2] ,您将有更多重复数据:

id  Brand  Price
0   1  volvo  20000
1   1   audi  30000
2   2  volvo  20000
3   2   audi  30000

This is my current solution:这是我目前的解决方案:

df1={'id':[1]}
df2 = {'Brand':['Volvo','Heizen','Eizen'],
        'Price':[20000,30000,40000]}

person=pd.DataFrame(df1)
car=pd.DataFrame(df2)
id=person.loc[0].id
car.insert(0,"id",id)
print(car)

This is my output:这是我的 output:

   id   Brand  Price
0   1   Volvo  20000
1   1  Heizen  30000
2   1   Eizen  40000

It can get my expected table.它可以得到我期望的桌子。 But is it has any better solution?但它有更好的解决方案吗?

df1 and df2 are not Pandas data frames. df1 和 df2 不是 Pandas 数据帧。

data1={'id':[1]}
data2={'Brand':['volvo','audi'],'Price':[20000,30000]} 

df1 = pd.DataFrame(data1) #creating dataframes
df2 = pd.DataFrame(data2)
frames = [df1,df2]

and concatenating并连接

pd.concat(frames, sort=False)

yields,产量,

id  Brand   Price
0   1.0     NaN     NaN
0   NaN     volvo   20000.0
1   NaN     audi    30000.0

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

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