Skip to content Skip to sidebar Skip to footer

Fill Other Columns With Missing Dates Nan Pandas Datafrane

I am actually extracting data from several excel files monitoring my daily calorie intake. I managed to use list comprehension to generate the dates. I tried to use merge or join a

Solution 1:

Build your dataframe with existing data and reindex it with missing dates

# Input data
date_list = ['2021-05-22','2021-05-24','2021-05-26','2021-05-27']
calories = [420,380,390,400,350,280,300,430]
duration = [50,40,45,50,45,50,44,58]

# Dataframe with sparse index
idx = pd.MultiIndex.from_product([pd.to_datetime([d for d in date_list]),
                                  ["Morning", "Afternoon"]],
                                 names=["Date", "Time"])
df = pd.DataFrame({'calories': calories, 'duration': duration}, index=idx)

# Dataframe with full index
idx1 = pd.MultiIndex.from_product([pd.date_range(date_list[0], date_list[-1]),
                                   ["Morning", "Afternoon"]],
                                  names=["Date", "Time"])
df1 = df.reindex(idx1).reset_index()
>>> df1
         Date       Time  calories  duration
0  2021-05-22    Morning     420.0      50.0
1  2021-05-22  Afternoon     380.0      40.0
2  2021-05-23    Morning       NaN       NaN
3  2021-05-23  Afternoon       NaN       NaN
4  2021-05-24    Morning     390.0      45.0
5  2021-05-24  Afternoon     400.0      50.0
6  2021-05-25    Morning       NaN       NaN
7  2021-05-25  Afternoon       NaN       NaN
8  2021-05-26    Morning     350.0      45.0
9  2021-05-26  Afternoon     280.0      50.0
10 2021-05-27    Morning     300.0      44.0
11 2021-05-27  Afternoon     430.0      58.0

Post a Comment for "Fill Other Columns With Missing Dates Nan Pandas Datafrane"