pandas 将带有日期时间索引的行插入到数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50840769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert row with datetime index to dataframe
提问by Freddy
I have a dataframe with datetime index:
我有一个带有日期时间索引的数据框:
time count day hour minute label
2018-06-07 00:25:00 207 7.0 0.0 25.0 177.0
2018-06-07 00:55:00 187 7.0 0.0 55.0 150.0
2018-06-07 01:25:00 194 7.0 1.0 25.0 165.0
2018-06-07 01:55:00 176 7.0 1.0 55.0 175.0
2018-06-07 02:25:00 195 7.0 2.0 25.0 172.0
-> add new datetime record record here
and I'm trying to add some new records but I get:
我正在尝试添加一些新记录,但我得到:
[DatetimeIndex(['2018-06-07 01:55:00'], dtype='datetime64[ns]', name='time', freq=None)] not an index
# this happen even if row exists or not
I just want to add a 30 minutes interval record, my code is something like
我只想添加一个 30 分钟的间隔记录,我的代码是这样的
last_date = recent_posts.iloc[[-1]].index
last_date = last_date + timedelta(minutes=30)
recent_posts.iloc[[last_date]] = # bla #bla
# What I may be doing wrong?
回答by harvpan
The correct way to insert a new record would be:
插入新记录的正确方法是:
df.append(pd.DataFrame(index=[last_date]))
Example:
例子:
print(df)
Output:
输出:
count day hour minute label
time
2018-06-07 00:25:00 207 7.0 0.0 25.0 177.0
2018-06-07 00:55:00 187 7.0 0.0 55.0 150.0
To add an element, use .append():
要添加元素,请使用.append():
df.append(pd.DataFrame(index=[last_date]))
Output:
输出:
count day hour label minute
2018-06-07 00:25:00 207.0 7.0 0.0 177.0 25.0
2018-06-07 00:55:00 187.0 7.0 0.0 150.0 55.0
2018-06-07 01:25:00 NaN NaN NaN NaN NaN
As you can see, it adds a new record with the defined index and since we did not specify values for other columns, they are NaN
如您所见,它添加了一个具有定义索引的新记录,并且由于我们没有为其他列指定值,因此它们是 NaN
You can specify the values for one or more columns with a dict
like this:
您可以为一列或多列指定值,dict
如下所示:
data = {'hour':10}
df.append(pd.DataFrame(data, index=[last_date]))
Output:
输出:
count day hour label minute
2018-06-07 00:25:00 207.0 7.0 0.0 177.0 25.0
2018-06-07 00:55:00 187.0 7.0 0.0 150.0 55.0
2018-06-07 01:25:00 NaN NaN 10.0 NaN NaN