Skip to content Skip to sidebar Skip to footer

Adding/inserting Values In Pandas Dataframe Based On 1 Or More Columns

I have 2 dataframes date sitename Auto_name AutoCount 2012-05-01 chess.com Autobiographer 8 2012-05-05 chess.com Aut

Solution 1:

You can eg use the merge function (see the docs on merging dataframes: http://pandas.pydata.org/pandas-docs/stable/merging.html). Assuming your dataframes are called df1 and df2:

In [13]: df = pd.merge(df1, df2, how='outer')

In [14]: df
Out[14]: 
         date   sitename       Auto_name  AutoCount Stu_name  StudentCount
02012-05-01  chess.com  Autobiographer          8  Student             412012-05-05  chess.com  Autobiographer          1NaNNaN22012-05-15  chess.com  Autobiographer          3NaNNaN32012-05-02  chess.com             NaNNaN  Student             2

Above it uses the common columns to merge on (in this case date and sitename), but you can also specify the columns with the on keyword (see docs).

In a next step you can fill the NaN values as you like. Following your example output, this can be:

In [15]: df.fillna({'Auto_name':'Autobiographer', 'AutoCount':0, 'Stu_name':'Student', 'StudentCount':0})
Out[15]: 
         date   sitename       Auto_name  AutoCount Stu_name  StudentCount
0  2012-05-01  chess.com  Autobiographer          8  Student             4
1  2012-05-05  chess.com  Autobiographer          1  Student             0
2  2012-05-15  chess.com  Autobiographer          3  Student             0
3  2012-05-02  chess.com  Autobiographer          0  Student             2

Post a Comment for "Adding/inserting Values In Pandas Dataframe Based On 1 Or More Columns"