1 year ago

#327641

test-img

mr17.5stuntman

how to to extend a dataframe that has date row index, from the last date in the dataframe till the end of the year? fill extended rows with nans

I have a CSV file that contains time series information from 1st January 2017 to March 8 2022.

What I'm trying to do is to create one a reusable function that can be used for different datasets (no hard coding of the parameters). The function should extend the given dataframe till the end of the current year. The extend rows should be filled with Nans.

Seceondly, replace the nan values of current year 2022 with data values of 2019 for matching index dates.

Thirdly, this operation should be done for all columns starting with the string T. Meanwhile for all the other columns that don't begin with the string T replace the nan values of the current year 2022 with data values of 2021 for matching index dates.

The output dataframe should have same number of columns as the original dataframe.

Below is an example of how the input dataset looks like: The date is a row index. There are 6 columns. I have also attached a screenshot of the tabular dataset. The dots (... or ..) just means there other values between the dates.

![enter image description here][1]

              Teacher   Torries  Task   Ego  Money  Workers
   date
01/01/2017     22        12       22     11   45     87
...            ..        ..       ..     ..   ..     ..
...            ..        ..       ..     ..   ..     ..
31/12/2017     79        21       17     91   28     60
01/01/2018     77        01       05     11   20     72
...            ..        ..       ..     ..   ..     ..
...            ..        ..       ..     ..   ..     ..
31/12/2018     02        19       54     36   78     97
01/01/2019     55        86       33     14   29     17
...            ..        ..       ..     ..   ..     ..
...            ..        ..       ..     ..   ..     ..
31/12/2019     12        13       14     15   09     01
01/01/2020     78        45       25     10   30     98
...            ..        ..       ..     ..   ..     ..
...            ..        ..       ..     ..   ..     ..
31/12/2020     32        17       47     58   89     21
01/01/2021     78        45       25     10   30     98
...            ..        ..       ..     ..   ..     ..
...            ..        ..       ..     ..   ..     ..
31/12/2021     02        19       88     23   97     14
01/01/2022     77        01       05     11   20     72
...            ..        ..       ..     ..   ..     ..
...            ..        ..       ..     ..   ..     ..
08/03/2022     17        68       74     21   01     04

python

pandas

date

indexing

extend

0 Answers

Your Answer

Accepted video resources