- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 MultiIndex 的 PANDAs DataFrame,其中一个级别代表一年:
import pandas as pd
df = pd.DataFrame(dict(A = ['foo', 'foo', 'bar', 'bar', 'bar', 'bar'],
B = ['white', 'black', 'white', 'white', 'black', 'black'],
year = [1990, 1992, 1990, 1992, 1991, 1992],
value = [3.14, 1.20, 4.56, 6.79, 0.01, 0.02]))
df = df.set_index(['A', 'B', 'year'])
我想向前填充值,但仅针对每个组中的中间年份(由 A 和 B 的交互定义)。这是输入:
value
A B year
foo white 1990 3.14
black 1992 1.20
bar white 1990 4.56
1992 6.79
black 1991 0.01
1992 0.02
这是所需的输出,还有一行:
value
A B year
foo white 1990 3.14
black 1992 1.20
bar white 1990 4.56
1991 4.56 <-- new forward-filled value
1992 6.79
black 1991 0.01
1992 0.02
我怎样才能简洁高效地完成这项工作?我试过使用 groupby
和 apply
的组合,但我是 PANDAS 的新手,一直抛出异常。
这是我如何天真地解决问题的示例:
def ffill_years(df):
df.reset_index(['A', 'B']) # drop all but 'year'
year_range = range(df['year'].min(), df['year'].max())
df.reindex(pd.Series(years)).fillna("ffill")
return df
df.groupby(level=['A', 'B']).apply(ffill_years)
当然这行不通。感谢所有提示!
最佳答案
你非常接近 - 几个小的变化:
reset_index
操作不到位.index
reindex
也没有就位method
见下文:
def ffill_years(df):
df = df.reset_index(['A','B']) # drop all but 'year'
year_range = range(df.index.min(), df.index.max() + 1)
df = df.reindex(pd.Series(year_range)).fillna(method='ffill')
return df
结果在
In [209]: df.groupby(level=['A','B']).apply(ffill_years)
Out[209]:
A B value
A B year
bar black 1991 bar black 0.01
1992 bar black 0.02
white 1990 bar white 4.56
1991 bar white 4.56
1992 bar white 6.79
foo black 1992 foo black 1.20
white 1990 foo white 3.14
关于python - MultiIndexed DataFrame 中的前向填充日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24810665/
我有一个包含 4 个级别的 MultiIndex 的 pandas 数据框。我正在尝试为每个 1 级索引选择具有不同 4 级索引的行。 例子: In [68]: df = pd.DataFrame({
我有一个数据框,其中包含 Year 和 Month 的多个索引,如下所示 | |Value Year |Month| | 1 | 3 1992 | 2 | 5
我有一个带有 3 级 MultiIndex 的 pandas 数据框。我正在尝试根据对应于两个级别的值列表提取此数据帧的行。 我有这样的事情: ix = pd.MultiIndex.from_prod
我想将具有多索引的数据帧中的所有列除以另一个具有多索引的数据帧,更小一级。前两个水平的蛾指数是相同的。并且应该播放第三级。 df_0 = pd.DataFrame( { "col0": [ 1
我想知道是否有人可以帮助我解决这个问题。 如果我有一个简单的数据框: one two three four 0 A 1 a 1 1 A 2 b
我希望创建一个新的 DataFrame,与基于芯片的设备 A 和 B 的结果相对应。 以下是我创建 DataFrame 的代码: import numpy as np import pandas as
我希望创建一个新的 DataFrame,与基于芯片的设备 A 和 B 的结果相对应。 以下是我创建 DataFrame 的代码: import numpy as np import pandas as
请让我知道如何将两个 DataFrame 与排序的 MultiIndexes 连接起来,以便结果具有排序的 MultiIndex。 由于两者都是排序的,算法必须根据两个 DataFrame 中的总行数
基本场景 对于推荐服务,我正在针对一组用户-项目交互训练矩阵分解模型 (LightFM)。为了使矩阵分解模型产生最佳结果,我需要将我的用户 ID 和商品 ID 映射到从 0 开始的连续整数 ID 范围
好的,所以我有一个数据框,其中包含时间序列数据,每列都有一个多行索引。这是数据的样例,它是 csv 格式的。加载数据在这里不是问题。 我想要做的是能够创建一个箱线图,其中包含根据多索引特定行中的不同类
我有这个数据框: df = pd.DataFrame({'NUMBER_1': {('2019-07', 'A'): 4, ('2019-07', 'D'): 2, ('2019-08', 'A'):
我有点难以对用于我的一个数据集的列的 pandas MultiIndex 进行排序: MultiIndex(levels=[['Jan', 'Feb', 'Mar', 'Apr', 'May', 'J
我有点难以对用于我的一个数据集的列的 pandas MultiIndex 进行排序: MultiIndex(levels=[['Jan', 'Feb', 'Mar', 'Apr', 'May', 'J
我有一个列表列表,我想将其制成多索引 Pandas 数据框,然后可以合并到原始 Pandas 数据框。列表的每一行都包含一个观察值,列表中的值由与该观察值匹配的相应行组成。 这是我所拥有的一个简单版本
我有以下 DataFrame(从我无法更改的 CSV 文件中读取): df = pd.DataFrame([['low', 0.5, 123, 0.8, 123],
我正在创建一个 MultiIndex.from_product(),但它必须是来自两个单独的 MultiIndexes 的唯一值的乘积。我下面的解决方案有效,但我想知道是否有更优雅的解决方案。 fro
我有一个很大的pd.DataFrame,a,看起来像: bid TIT IM Equity HELN SE Equity FHZN SE Equity GLEN LN Equity f
我创建了一个带有多索引的数据框,其中一个索引是一个元组。我无法使用 .loc 进行索引。 这是数据框: a = [['a','a','a','b','b','b','c','c'],[('one',2
我有以下数据框 A B C D E F Algo T X
我在使用 MultiIndex 和 stack() 时遇到问题。以下示例基于 solution from Calvin Cheung在 StackOvervlow 上。 === multi.csv =
我是一名优秀的程序员,十分优秀!