python - Pandas :从MultiIndex中的日期选择

标签 python pandas

假设我有 MultiIndex 系列作为

date        foo
2006-01-01  1         12931926.310
            3         11084049.460
            5         10812205.359
            7          9031510.239
            9          5324054.903
2007-01-01  1         11086082.624
            3         12028419.560
            5         11957253.031
            7         10643307.061
            9          6034854.915

如果它不是 MultiIndex,我可以选择年份为 2007 的那些通过df.loc['2007'] .我该怎么做?我的自然猜测是 df.loc['2007', :] ,但这给了我一个空的 Series([], name: FINLWT21, dtype: float64) .

最终目标

最终,我也有兴趣替换与 2007 不同日期的所有行。与年份的行2007 .

也就是说,我的预期输出是

date        foo
2006-01-01  1         11086082.624
            3         12028419.560
            5         11957253.031
            7         10643307.061
            9          6034854.915
2007-01-01  1         11086082.624
            3         12028419.560
            5         11957253.031
            7         10643307.061
            9          6034854.915

我尝试实现@unutbu 的解决方案,但是

mySeries.loc[dateIndex.year != 2007] = mySeries.loc[dateIndex.year == 2007]

自然会将值(由于 RHS 上不存在)设置为 NaN .通常,这些问题由

mySeries.loc[dateIndex.year != 2007] = mySeries.loc[dateIndex.year == 2007].values

,但考虑到我有 10左侧的值(以及我的真实数据集中的更多值),但只有 5在右边,我得到

ValueError: cannot set using a list-like indexer with a different length than the value

我现在想到的唯一替代方案是迭代第一个索引,然后对每个子组使用之前的命令,但这似乎不是最有效的解决方案。

最佳答案

给定系列

In [207]: series
Out[212]: 
date        foo
2006-01-01  1      12931926.310
            3      11084049.460
            5      10812205.359
            7       9031510.239
            9       5324054.903
2007-01-01  1      11086082.624
            3      12028419.560
            5      11957253.031
            7      10643307.061
            9       6034854.915
Name: val, dtype: float64

您可以提取 date 索引

dateindex = series.index.get_level_values('date')
# Ensure the dateindex is a DatetimeIndex (as opposed to a plain Index)
dateindex = pd.DatetimeIndex(dateindex)

现在可以选择年份等于 2007 的行 bool 条件:

# select rows where year equals 2007
series2007 = series.loc[dateindex.year == 2007]

如果 foo 值在每个日期以相同的顺序循环通过相同的值, 那么您可以将系列中的所有值替换为 2007 年的值

N = len(series)/len(series2007)
series[:] = np.tile(series.loc[dateindex.year == 2007].values, N)

使用 np.tile.values 的一个优点是它会相对快速地生成所需的值数组。一个(可能的)缺点是它忽略了索引,因此它依赖于 foo 值在每个日期以相同顺序循环通过相同值的假设。

更健壮(但更慢)的方法是使用连接:

df = series.reset_index('date')
df2007 = df.loc[dateindex.year==2007]
df = df.join(df2007, rsuffix='_2007')
df = df[['date', 'val_2007']]
df = df.set_index(['date'], append=True)
df = df.swaplevel(0,1).sort_index()     

产量

In [304]: df.swaplevel(0,1).sort_index()
Out[304]: 
                    val_2007
date       foo              
2006-01-01 1    11086082.624
           3    12028419.560
           5    11957253.031
           7    10643307.061
           9     6034854.915
2007-01-01 1    11086082.624
           3    12028419.560
           5    11957253.031
           7    10643307.061
           9     6034854.915
2008-01-01 1    11086082.624
           3    12028419.560
           5    11957253.031
           7    10643307.061
           9     6034854.915

关于python - Pandas :从MultiIndex中的日期选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29458674/

相关文章:

python - 我如何比较 pandas 中具有相同类别的两个日期列

python - Mapreduce Job 查找 python 中的词频计数

javascript - 不懂Node.js架构

python - PyMYSQL - 选择值可以为 NULL 的位置

python - 检查服务器是否在线的合理超时是多少?

python - 在 2D numpy 数组中有效地找到正值的索引范围

python - 如何在 Pandas 中汇总一列

python - Pandas:使用时间序列作为选择的掩码

python - Pandas 错误 : Index contains duplicate entries, 无法 reshape

python - 如何创建一个列来跟踪另一列中的值在 pandas 中的该行之前出现的次数