Python function to add values in a Pandas Dataframe using values from another Dataframe
NickName: Ask DateTime:2015-11-21T03:00:12

Python function to add values in a Pandas Dataframe using values from another Dataframe

I am a newbie in Python and I am struggling for coding things that seem simple in PHP/SQL and I hope you can help me.

I have 2 Pandas Dataframes that I have simplified for a better understanding.

In the first Dataframe df2015, I have the Sales for the 2015. ! Notice that unfortunately, we do not have ALL the values for each store !

>>> df2015

    Store   Date        Sales       
0   1       2015-01-15  6553        
1   3       2015-01-15  7016        
2   6       2015-01-15  8840    
3   8       2015-01-15  10441
4   9       2015-01-15  7952

And another Dataframe named df2016 for the Sales Forecast in 2016, which lists ALL the stores.
( As you guess, the column SalesForecast is the column to fill. )

>>> df2016

    Store   Date        SalesForecast
0   1       2016-01-15      
1   2       2016-01-15  
2   3       2016-01-15  
3   4       2016-01-15  
4   5       2016-01-15  

I want to create a function that for each row in df2016 will retrieve the Sales values from df2015, and for example, will increase by 5% these values and add these new values in SalesForecast column of df2016.

Let's say forecast is the function I have created that I want to apply :

def forecast(store_id,date):
    sales2015 = df2015['Sales'].loc[(df2015['Store'].values == store_id) & (df2015['Date'].values == date )].values
    forecast2016 = sales2015 * 1.05
    return forecast2016

I have tested this function in a hardcoding way as below and it works:

>>> forecast(1,'2015-01-15')
array([ 6880.65])

But here we are where my problem is... How can I apply this function to the dataframes ?

It would be very easy to do it in PHP by creating a loop for each row in df2016 and retrieve the values (if they exist) from df2015 by a SELECT and WHERE Store = store_id and Date = date.. ...but the it seems the logic is not the same with Pandas Dataframes and Python.

I have tried the apply function as follows :

df2016['SalesForecast'] = df2016.apply(df2016['Store'],df2016['Date'])

but I am unable to put the arguments correctly or there is something I am doing wrong..

I think I do not have the good method or maybe my method is not suitable at all with Pandas and Python.. ?

Copyright Notice:Content Author:「」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/33834136/python-function-to-add-values-in-a-pandas-dataframe-using-values-from-another-da

More about “Python function to add values in a Pandas Dataframe using values from another Dataframe” related questions

Python function to add values in a Pandas Dataframe using values from another Dataframe

I am a newbie in Python and I am struggling for coding things that seem simple in PHP/SQL and I hope you can help me. I have 2 Pandas Dataframes that I have simplified for a better understanding. ...

Show Detail

Pandas fill missing values in dataframe from another dataframe

I cannot find a pandas function (which I had seen before) to substitute the NaN's in a dataframe with values from another dataframe (assuming a common index which can be specified). Any help?

Show Detail

Pandas fill missing values in dataframe from another dataframe

I cannot find a pandas function (which I had seen before) to substitute the NaN's in a dataframe with values from another dataframe (assuming a common index which can be specified). Any help?

Show Detail

Pandas fill missing values in dataframe from another dataframe

I cannot find a pandas function (which I had seen before) to substitute the NaN's in a dataframe with values from another dataframe (assuming a common index which can be specified). Any help?

Show Detail

Updating values in a pandas dataframe using another dataframe

I have an existing pandas Dataframe with the following format: sample_dict = {'ID': [100, 200, 300], 'a': [1, 2, 3], 'b': [.1, .2, .3], 'c': [4, 5, 6], 'd': [.4, .5, .6]} df_sample = pd.DataFr

Show Detail

How do I offset a dataframe with values in another dataframe?

I have two dataframes. One is the basevales (df) and the other is an offset (df2). How do I create a third dataframe that is the first dataframe offset by matching values (the ID) in the second dat...

Show Detail

Fill pandas dataframe rows from values in another dataframe rows

I have two pandas dataframes as given below: df1 Name City Postal_Code State James Phoenix 85003 AZ John Scottsdale 85259 AZ Jeff Phoenix 850...

Show Detail

Replacing values in one pandas dataframe with values from another dataframe

I have to replace values from one dataframe with values from another dataframe. Example bellow works, but I have extra steps in order to replace values in "first" column with values from "new" col...

Show Detail

Removing the values from a smaller dataframe from a larger dataframe in pandas

I have a very large dataframe in pandas where one of the columns is labeled "Col2" and the row values for this column contain a long string. I parsed out of this dataframe another smaller dataframe...

Show Detail

Fill Nulls with values in another dataframe in pandas

I have two dataframes. In dataframe1, I have a column with some Null values. I want to fill those null values using the values of another data frame i.e.,dataframe2 by comparing of values of differ...

Show Detail