I have a dataframe that looks like this:
Name A B C
D1 1 3 3
D2 2 4 4
D3 2 1 1
How can I create a new dataframe of the same size where every value is today's date minus the value of my first dataframe?
for example, if today is 2018-04-27, my new dataframe would look like this:
Name A B C
D1 2018-04-26 2018-04-24 2018-04-24
D2 2018-04-25 2018-04-23 2018-04-23
D3 2018-04-25 2018-04-26 2018-04-26
I'm thinking the solution will include something like
df2.iloc[1,1] = datetime.today() - timedelta(days=df1[1,1])
but I'm running into all kinds of type errors and problems looping through the original df
import datetime as dt
from datetime import timedelta
import pandas as pd
df = pd.DataFrame({'Name':['D1','D2','D3'],'A':[1,2,2],'B':[3,4,1],'C':[3,4,1]})
df.set_index('Name', inplace=True)
df2 = df.applymap(lambda x: dt.date.today() - timedelta(days = x))
df2
A B C
Name
D1 2018-04-26 2018-04-24 2018-04-24
D2 2018-04-25 2018-04-23 2018-04-23
D3 2018-04-25 2018-04-26 2018-04-26
Applymap is what you're looking to use
Edit: adding imports so that you avoid issues with datetime imports as seen here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With