我有以下pandas
数据框:
import numpy as np
import pandas as pd
timestamps = [1, 14, 30]
data = dict(quantities=[1, 4, 9], e_quantities=[1, 2, 3])
df = pd.DataFrame(data=data, columns=data.keys(), index=timestamps)
如下所示:
quantities e_quantities
1 1 1
14 4 2
30 9 3
但是,timestamps
应该从1到52:
index = pd.RangeIndex(1, 53)
以下行提供了缺少的timestamps
:
series_fill = pd.Series(np.nan, index=index.difference(df.index)).sort_index()
如何在这些缺少的时间戳上使quantities
和e_quantities
列具有NaN值?
我尝试过:
df = pd.concat([df, series_fill]).sort_index()
但是它添加了另一列(0
)并交换了原始数据帧的顺序:
0 e_quantities quantities
1 NaN 1.0 1.0
2 NaN NaN NaN
3 NaN NaN NaN
感谢您的帮助。
答案 0 :(得分:3)
我认为您正在寻找reindex
df=df.reindex(index)