简体   繁体   English

如何将行添加到数据框中以组织时间序列

[英]How to add rows into data frame in order to organize time series

I have a data frame that looks like this 我有一个看起来像这样的数据框

        AUX     TER
11/2014 2.0     10.0
01/2015 23.0    117.0
03/2015 57.0    65.0
04/2015 1.0     1.0
05/2015 16.0    20.0
07/2015 19.0    30.0

I want to fill the values for months that are not in data frame with 0 like this 我想填写数据框中没有数字框架的数值,如下所示

        AUX     TER
11/2014 2.0     10.0
12/2014 0       0
01/2015 23.0    117.0
03/2015 57.0    65.0
04/2015 1.0     1.0
05/2015 16.0    20.0
06/2015 0       0
07/2015 19.0    30.0
  1. Change your index to datetime 将索引更改为datetime

     df.index = pd.to_datetime(df.index, format='%m/%Y') 
  2. Use asfreq with the fill_value argument asfreqfill_value参数一起使用

     df.asfreq('MS', fill_value=0) AUX TER 2014-11-01 2.0 10.0 2014-12-01 0.0 0.0 2015-01-01 23.0 117.0 2015-02-01 0.0 0.0 2015-03-01 57.0 65.0 2015-04-01 1.0 1.0 2015-05-01 16.0 20.0 2015-06-01 0.0 0.0 2015-07-01 19.0 30.0 

You can use the below to reindex() : 您可以使用以下重新reindex()

s=pd.to_datetime(df.index)
df.reindex(pd.date_range(s.min(),s.max()+pd.DateOffset(months=1),freq='M')
           .strftime('%m/%Y'),fill_value=0)

          AUX    TER
11/2014   2.0   10.0
12/2014   0.0    0.0
01/2015  23.0  117.0
02/2015   0.0    0.0
03/2015  57.0   65.0
04/2015   1.0    1.0
05/2015  16.0   20.0
06/2015   0.0    0.0
07/2015  19.0   30.0

Using df.resample("M").mean().fillna(0) 使用df.resample("M").mean().fillna(0)

Ex: 例如:

df = pd.read_csv(filename, sep="\s+", parse_dates=['date'])
df.set_index("date", inplace=True)
df = df.resample("M").mean().fillna(0)
df.index = df.index.strftime("%m/%Y")

print(df)

Output: 输出:

          AUX    TER
11/2014   2.0   10.0
12/2014   0.0    0.0
01/2015  23.0  117.0
02/2015   0.0    0.0
03/2015  57.0   65.0
04/2015   1.0    1.0
05/2015  16.0   20.0
06/2015   0.0    0.0
07/2015  19.0   30.0

When you have a datetime format, you can try: 如果您有日期时间格式,可以尝试:

df.resample('MS').mean()

following this post: Python, summarize daily data in dataframe to monthly and quarterly 关注这篇文章: Python,将每日数据汇总到每月和每季度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM