Python: How to use or get the column header in Pandas and use it as input/value

Viewed 2512

I have the following script that produces an empty DataFrame using Pandas:

import pandas as pd
import datetime

#Creating a list of row headers with dates.
start=datetime.date(2017,3,27)
end=datetime.date.today() - datetime.timedelta(days=1)
row_dates=[x.strftime('%m/%d/%Y') for x in pd.bdate_range(start,end).tolist()]
identifiers=['A','B','C']
#Creating an empty Dataframe
df=pd.DataFrame(index=identifiers, columns=row_dates)

print(df)

Now, suppose I have a function (let's call it "my_function(index,date)") that requires two inputs: an index, and a date.

That function gives me an outcome and I want to fill the corresponding empty slot of the dataframe with that outcome. But for that, I need to be able to acquire both the index and the date.

For example, let's say I want to fill the first slot of my DataFrame, I require index 'A' and the first Date which is '27/3/2017', so I have this:

my_function('A', '27/3/2017')

How can I make that happen for my entire DataFrame? My apologies if any of this sounds confusing.

1 Answers
Related