Python:merge data frame with different rows

时间:2017-06-14 10:29:37

标签: python dataframe merge

I need to merge two data frame with different rows and without common key:

df1:

name | age | loc

Bob | 20 | USA

df2:

food | car | sports

Sushi | Toyota | soccer

meat | Ford | baseball

result I want:

name | age | loc | food | car | sports

Bob | 20 | USA | Sushi | Toyota | soccer

Bob | 20 | USA | Meat | Ford | baseball

my code below:

pd.merge(df1,df2,how='right',left_index=True,right_index=True)

it works well when df2 is more than two rows but be incorrect when df2 is only one row.

any ideas for this question?

2 个答案:

答案 0 :(得分:1)

df2的索引使用reindex_axis

df1 = df1.reindex_axis(df2.index, method='ffill')
print (df1)
  name  age  loc
0  Bob   20  USA
1  Bob   20  USA

df = pd.merge(df1,df2,how='right',left_index=True,right_index=True)
print (df)
  name  age  loc   food     car    sports
0  Bob   20  USA  Sushi  Toyota    soccer
1  Bob   20  USA   meat    Ford  baseball

如果ffill.ffill中没有NaN数据,则您可以使用fillna方法df1df2):

#default outer join
df = pd.concat([df1,df2], axis=1).ffill()
print (df)
  name   age  loc   food     car    sports
0  Bob  20.0  USA  Sushi  Toyota    soccer
1  Bob  20.0  USA   meat    Ford  baseball
df = pd.merge(df1,df2,how='right',left_index=True,right_index=True).ffill()
print (df)
  name   age  loc   food     car    sports
0  Bob  20.0  USA  Sushi  Toyota    soccer
1  Bob  20.0  USA   meat    Ford  baseball

答案 1 :(得分:1)

另一种解决方案......基于concat。

x = range(0,5)
y = range(5,10)
z = range(10,15)
a = range(10,5,-1)
b = range(15,10,-1)
v = range(0,1)
w = range(2,3)

A = pd.DataFrame(dict(x=x,y=y,z=z))
B = pd.DataFrame(dict(a=a,b=b))
C = pd.DataFrame(dict(v=v,w=w))

pd.concat([A,B])
>>> pd.concat([A,B],axis = 1)
   x  y   z   a   b
0  0  5  10  10  15
1  1  6  11   9  14
2  2  7  12   8  13
3  3  8  13   7  12
4  4  9  14   6  11

@Edit:基于评论..这个解决方案没有回答问题。因为在问题中行数是不同的。这是另一种解决方案 该解决方案基于数据帧D

n_mult = B.shape[0]
D = C.append([C]*(n_mult-1)).reset_index()[['v','w']]
pd.concat([D,B],axis = 1)