【问题标题】:expand mid year values to month in pandas在熊猫中将年中值扩展到月份
【发布时间】:2019-10-15 07:52:48
【问题描述】:

来自expand year values to month in pandas

我有:

pd.DataFrame({'comp':['a','b'], 'period':['20180331','20171231'],'value':[12,24]})
    comp    period  value
0   a   20180331    12
1   b   20171231    24

并且想推断到 201701 到 201812(含)。该值应分布在该期间之前的 12 个月内。

comp yyymm value
a    201701 na
a    201702 na
...
a    201705 12
a    201706 12
...
a    201803 12
a    201804 na
b    201701 24
...
b    201712 24
b    201801 na
...

【问题讨论】:

  • 您想推断每个 comp 的唯一值吗?
  • @Parth 正确,适用于每家公司
  • 如果样本数据中每组只有一个值,您能否解释更多推断?

标签: pandas reshape


【解决方案1】:

用途:

#create month periods with min and max value
r = pd.period_range('2017-01', '2018-12', freq='m')
#convert column to period
df['period'] = pd.to_datetime(df['period']).dt.to_period('m')
#create MultiIndex for add all possible values
mux = pd.MultiIndex.from_product([df['comp'], r], names=('comp','period'))
#reindex for append values
df = df.set_index(['comp','period'])['value'].reindex(mux).reset_index()

#back filling by 11 values of missing values per groups
df['new'] = df.groupby('comp')['value'].bfill(limit=11)

print (df)

   comp   period  value   new
0     a  2017-01    NaN   NaN
1     a  2017-02    NaN   NaN
2     a  2017-03    NaN   NaN
3     a  2017-04    NaN  12.0
4     a  2017-05    NaN  12.0
...
...
10    a  2017-11    NaN  12.0
11    a  2017-12    NaN  12.0
12    a  2018-01    NaN  12.0
13    a  2018-02    NaN  12.0
14    a  2018-03   12.0  12.0
15    a  2018-04    NaN   NaN
16    a  2018-05    NaN   NaN
17    a  2018-06    NaN   NaN
18    a  2018-07    NaN   NaN
19    a  2018-08    NaN   NaN
20    a  2018-09    NaN   NaN
21    a  2018-10    NaN   NaN
22    a  2018-11    NaN   NaN
23    a  2018-12    NaN   NaN
24    b  2017-01    NaN  24.0
25    b  2017-02    NaN  24.0
26    b  2017-03    NaN  24.0
...
...
32    b  2017-09    NaN  24.0
33    b  2017-10    NaN  24.0
34    b  2017-11    NaN  24.0
35    b  2017-12   24.0  24.0
36    b  2018-01    NaN   NaN
37    b  2018-02    NaN   NaN
38    b  2018-03    NaN   NaN
...
...
44    b  2018-09    NaN   NaN
45    b  2018-10    NaN   NaN
46    b  2018-11    NaN   NaN
47    b  2018-12    NaN   NaN

【讨论】:

    【解决方案2】:

    看看这是否有效:

    dftime = pd.DataFrame(pd.date_range('20170101','20181231'), columns=['dt']).apply(lambda x: x.dt.strftime('%Y-%m'), axis=1) # Populating full range including dates
    dftime = dftime.assign(dt=dftime.dt.drop_duplicates().reset_index(drop=True)).dropna()  # Dropping duplicates from above range
    
    df['dt'] = pd.to_datetime(df.period).apply(lambda x: x.strftime('%Y-%m'))  # Adding column for merging purpose
    target = df.groupby('comp').apply(lambda x: dftime.merge(x[['comp','dt','value']], on='dt', how='left').fillna({'comp':x.comp.unique()[0]})).reset_index(drop=True) # Populating data for each company
    

    这给出了所需的输出: print(target)

    dt comp  value
    0   2017-01    a    NaN
    1   2017-02    a    NaN
    2   2017-03    a    NaN
    3   2017-04    a    NaN
    4   2017-05    a    NaN
    5   2017-06    a    NaN
    6   2017-07    a    NaN
    

    等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2022-07-06
      • 1970-01-01
      • 2016-06-24
      • 2020-10-06
      • 1970-01-01
      • 2017-06-14
      相关资源
      最近更新 更多