python - How to update a column in Pandas Dataframe -


in pandas, trying add new column / update existing column data frame (df2) value data frame (df1). can think of how in sql

update df2 set df2['column'] = df1['column'] df2 join df1 on df1['nonindexcolumn'] = df2['nonindexcolumn'] 

data example:

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) 

i trying add df1['signupdate'] on df2, df2 this:

       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 

i know merge allow me add column, have either overwrite existing df or create new one, this:

df1 = pd.merge(df1, df2) #overwrite df3 = pd.merge(df1, df2) #new dataframe 

is there not way join on 1 field (maybe indexed column, maybe not indexed column) , update / add field?

perform left 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 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -