Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shift multiple columns of data to the left direction to fill empty values

Given a dataframe df as follows, note that for each date column, we have 3 not-na values:

         date      value     20211003     20211010     20211017     20211024
0   2021-9-19  3613.9663          NaN          NaN          NaN          NaN
1   2021-9-26  3613.0673          NaN          NaN          NaN          NaN
2   2021-10-3  3568.1668          NaN          NaN          NaN          NaN
3  2021-10-10  3592.1666  3631.411799          NaN          NaN          NaN
4  2021-10-17  3572.3662  3637.792491  3677.656329          NaN          NaN
5  2021-10-24  3582.6036  3678.800911  3707.926324  3712.669694          NaN
6  2021-10-31  3547.3361          NaN  3731.336899  3735.695071  3733.021293
7   2021-11-7  3491.5677          NaN          NaN  3749.422639  3743.507496
8  2021-11-14  3539.1002          NaN          NaN          NaN  3737.851787
9  2021-11-21  3560.3734          NaN          NaN          NaN          NaN

I would like to reshape it to a new dataframe with columns: date, value, pred1, pred2, pred3 by: shifting date columns (20211003, 20211010, 20211017, 20211024) of data to the left direction to fill empty values.

The expected result will like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

         date      value        pred1        pred2        pred3
0   2021-9-19  3613.9663          NaN          NaN          NaN
1   2021-9-26  3613.0673          NaN          NaN          NaN
2   2021-10-3  3568.1668          NaN          NaN          NaN
3  2021-10-10  3592.1666  3631.411799          NaN          NaN
4  2021-10-17  3572.3662  3637.792491  3677.656329          NaN
5  2021-10-24  3582.6036  3678.800911  3707.926324  3712.669694
6  2021-10-31  3547.3361  3731.336899  3735.695071  3733.021293
7   2021-11-7  3491.5677  3749.422639  3743.507496          NaN
8  2021-11-14  3539.1002  3737.851787          NaN          NaN
9  2021-11-21  3560.3734          NaN          NaN          NaN

Anyone could help to deal this issue? Thanks.

>Solution :

df.iloc[:,:2].join(pd.DataFrame(df.iloc[:, 2:].agg(lambda x: list(x.dropna()), axis=1).to_list()))

         date      value            0            1            2
0   2021-9-19  3613.9663          NaN          NaN          NaN
1   2021-9-26  3613.0673          NaN          NaN          NaN
2   2021-10-3  3568.1668          NaN          NaN          NaN
3  2021-10-10  3592.1666  3631.411799          NaN          NaN
4  2021-10-17  3572.3662  3637.792491  3677.656329          NaN
5  2021-10-24  3582.6036  3678.800911  3707.926324  3712.669694
6  2021-10-31  3547.3361  3731.336899  3735.695071  3733.021293
7   2021-11-7  3491.5677  3749.422639  3743.507496          NaN
8  2021-11-14  3539.1002  3737.851787          NaN          NaN
9  2021-11-21  3560.3734          NaN          NaN          NaN

In steps:

pd.DataFrame(df.iloc[:, 2:].agg(lambda x: list(x.dropna()), axis=1).to_list())
Out[57]: 
             0            1            2
0          NaN          NaN          NaN
1          NaN          NaN          NaN
2          NaN          NaN          NaN
3  3631.411799          NaN          NaN
4  3637.792491  3677.656329          NaN
5  3678.800911  3707.926324  3712.669694
6  3731.336899  3735.695071  3733.021293
7  3749.422639  3743.507496          NaN
8  3737.851787          NaN          NaN
9          NaN          NaN          NaN

Then join/concatenate/ to the original first 2 columns

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading