在 pandas 数据框中插入新行

Inserting a new row in pandas dataframe

我有一个如下所示的数据集-

               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
....

此数据框缺少一些行,我想插入这些行及其前一行的值。下面是我试图在 2012-12-31 处插入值的代码,该值等于 2012-12-30.

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

但这不会在数据框中添加任何行,这会导致 nan 值 -

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

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

当我在 2016 年做同样的事情时,它没有添加任何东西-

    Prec    Tmax    Tmin    Tmean
Date_____________________________           

如何插入一行?

因此,给定以下玩具数据框:

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,
        },
    }
)

你可以这样做:

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