如何更新Pandas Dataframe中的列

时间:2015-04-14 16:32:37

标签: python pandas

Pandas中,我尝试使用来自另一个数据帧(DF1)的值添加新列/将现有列更新到数据帧(DF2)。我可以在SQL

中考虑如何执行此操作
UPDATE DF2
SET DF2['Column'] = DF1['Column']
FROM DF2
JOIN DF1 ON DF1['NonIndexColumn'] = DF2['NonIndexColumn']

数据示例:

d =[{'CustomerID': 1, 'SignUpDate': '2014-01-01'}, {'CustomerID': 2, 'SignUpDate': '2014-02-01'}, {'CustomerID': 3, 'SignUpDate': '2014-03-01'}, {'CustomerID': 4, 'SignUpDate': '2014-04-01'}]
DF1 = pd.DataFrame(data=d)

d2 = [{'OrderID': 1, 'CustomerID': 1, 'OrderDate': '2014-01-15'}, {'OrderID': 2, 'CustomerID': 1, 'OrderDate': '2014-01-15'}, {'OrderID': 3, 'CustomerID': 2, 'OrderDate': '2014-03-28'}, {'OrderID': 4, 'CustomerID': 1, 'OrderDate': '2014-03-29'}, {'OrderID': 5, 'CustomerID': 3, 'OrderDate': '2014-04-28'}, {'OrderID': 6, 'CustomerID': 2, 'OrderDate': '2014-06-01'}, {'OrderID': 7, 'CustomerID': 1, 'OrderDate': '2014-11-06'}, {'OrderID': 8, 'CustomerID': 3, 'OrderDate': '2015-01-28'}, {'OrderID': 9, 'CustomerID': 1, 'OrderDate': '2015-02-15'} ]
DF2 = pd.DataFrame(data=d2)

我正在尝试将DF1['SignUpDate']添加到DF2,以便DF2看起来像这样:

       CustomerID   OrderDate  OrderID  SignUpDate
0           1  2014-01-15        1      2014-01-01
1           1  2014-01-15        2      2014-01-01
2           2  2014-03-28        3      2014-02-01
3           1  2014-03-29        4      2014-01-01
4           3  2014-04-28        5      2014-03-01
5           2  2014-06-01        6      2014-02-01
6           1  2014-11-06        7      2014-01-01
7           3  2015-01-28        8      2014-03-01
8           1  2015-02-15        9      2014-01-01

我知道merge允许我添加列,但我必须覆盖现有的DF或创建一个新的,如下所示:

DF1 = pd.merge(DF1, DF2) #overwrite
DF3 = pd.merge(DF1, DF2) #new dataframe

是否有办法加入一个字段(可能是索引列,可能不是索引列)并更新/添加字段?

1 个答案:

答案 0 :(得分:1)

执行左merge

In [4]:

DF2.merge(DF1, on='CustomerID', how='left')
Out[4]:
   CustomerID   OrderDate  OrderID  SignUpDate
0           1  2014-01-15        1  2014-01-01
1           1  2014-01-15        2  2014-01-01
2           2  2014-03-28        3  2014-02-01
3           1  2014-03-29        4  2014-01-01
4           3  2014-04-28        5  2014-03-01
5           2  2014-06-01        6  2014-02-01
6           1  2014-11-06        7  2014-01-01
7           3  2015-01-28        8  2014-03-01
8           1  2015-02-15        9  2014-01-01