lsr729
lsr729

Reputation: 832

Inserting a new row in pandas dataframe

I have a dataset that looks like the below-

               Prec         Tmax       Tmin      Tmean
Date                
2010-01-01  0.585135    3.901162    -2.057929   0.921617
2010-01-02  0.100535    -3.498832   -8.125136   -5.811984
2010-01-03  0.123689    -3.635599   -11.228860  -7.432230
2010-01-04  0.616149    -2.582895   -10.813682  -6.698288
2010-01-05  0.371212    -2.538388   -9.403697   -5.971043
....

This dataframe has some rows missing and I would like to insert these rows with the values of their previous row. Below is the code I am trying to insert the values at 2012-12-31 that would be equal to the values of 2012-12-30.

climate_al_new.loc["2012-12-31"] = climate_al_new.loc["2012-12-30"]

But this does not add any row in the dataframe, this results in nan values-

climate_al_new.loc["2012-12-31"]

>           Prec Tmax Tmin Tmean
Date                
2012-12-31  NaN NaN NaN NaN

And when I do the same for the year 2016, it does not add anything-

    Prec    Tmax    Tmin    Tmean
Date_____________________________           

How can I insert a row?

Upvotes: 2

Views: 3646

Answers (1)

Laurent
Laurent

Reputation: 13458

So, given the following toy dataframe:

import pandas as pd

df = pd.DataFrame(
    {
        "Prec": {
            "2010-01-01": 0.585135,
            "2012-12-30": 0.100535,
        },
        "Tmax": {
            "2010-01-01": 3.901162,
            "2012-12-30": -3.498832,
        },
        "Tmin": {
            "2010-01-01": -2.057929,
            "2012-12-30": -8.125136,
        },
        "Tmean": {
            "2010-01-01": 0.921617,
            "2012-12-30": -5.811984,
        },
    }
)

You can do it like this:

df.index = pd.to_datetime(df.index)

new_row = df.copy()[df.index == "2012-12-30"]

new_row.index = new_row.index + pd.Timedelta(days=1)

df = pd.concat([df, new_row]).sort_index(ignore_index=True)

print(df)
# Output
                Prec      Tmax      Tmin     Tmean
2010-01-01  0.585135  3.901162 -2.057929  0.921617
2012-12-30  0.100535 -3.498832 -8.125136 -5.811984
2012-12-31  0.100535 -3.498832 -8.125136 -5.811984

Upvotes: 1

Related Questions