pandas:创建一个左连接并添加行

时间:2015-07-24 20:58:41

标签: python-2.7 pandas

我想和两个Datafame一起加入

public class MyAppilcation extends Application{
...
}

enter image description here enter image description here

预期的结果是:

left = DataFrame({'Title': ['Paris Match', 'Lancome', 'Channel'],
                   'City': ['Paris', 'Milan', 'Montpellier']})


right = DataFrame({'Title': ['Lulu', 'Channel', 'Balance', 'Paris Match', 'Shaq', 'And 1'],
                  'City': ['New york', 'Valparaiso' ,'Montreal', 'Paris', 'Los Angeles', 'Brooklyn'],
                  'Price':[10,20,30,40,50,60]})

enter image description here

我正在r = DataFrame({'Title': ['Paris Match', 'Lancome', 'Channel','Lulu', 'Balance', 'Shaq', 'And 1'], 'City': ['Paris', 'Milan', 'Montpellier', 'Montreal', 'Paris', 'Los Angeles', 'Brooklyn'], 'Price':[40,'NaN',30,40,50,60,'Nan']}) r[['Title', 'City', 'Price']] ,我在result = left.join(right)

上收到columns overlap错误

1 个答案:

答案 0 :(得分:2)

执行外部merge

In [30]:

left.merge(right, how='outer')
Out[30]:
          City        Title  Price
0        Paris  Paris Match     40
1        Milan      Lancome    NaN
2  Montpellier      Channel    NaN
3     New york         Lulu     10
4   Valparaiso      Channel     20
5     Montreal      Balance     30
6  Los Angeles         Shaq     50
7     Brooklyn        And 1     60