'Pandas Dataframe - Add a new Column with value from another row

I am trying to add a new column called ordered_1day_ago to my df.

DataFrame currently looks like this:

itemID orderedDate qty
1 12/2/21 3
2 12/3/21 2
1 12/3/21 2
1 12/4/21 3

I want it to look like this:

itemID orderedDate qty ordered_1day_ago
1 12/2/21 3 0
2 12/3/21 2 0
1 12/3/21 2 3
1 12/4/21 3 2

itemID and ordered date must be used to insert the qty on the next orderedDate if it falls within one day, if it does not, then ordered_1day_ago is 0.

How can we use pandas for this?



Solution 1:[1]

This is the complete solution:

import pandas as pd

# a dict to create th dataframe
d = {
    'itemID':[1,2,1,1], 
    'orderedDate':['12/2/21', '12/3/21', '12/3/21', '12/4/21'],
    'qty':[3,2,2,3]
    }

# the old dataframe
df = pd.DataFrame(d)
print(df)

# some function to do what you want to based on rows
def some_function(row):
    # code goes here
    z = row['itemID'] + row['qty']
    return z

# add the new column given the function above
df['ordered_1day_ago'] = df.apply(some_function, axis=1)


# the new datafdrame with the extra column
print(df)

This is the original df:

   itemID orderedDate  qty
0       1     12/2/21    3
1       2     12/3/21    2
2       1     12/3/21    2
3       1     12/4/21    3

This is the new df with the added (example) column:

   itemID orderedDate  qty  ordered_1day_ago
0       1     12/2/21    3                 4
1       2     12/3/21    2                 4
2       1     12/3/21    2                 3
3       1     12/4/21    3                 4

You can amend the function to contain whatever criteria you wish such that the new column ordered_1day_ago contains the results that you wish.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 D.L