- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图填充所有缺失值,直到数据帧结束,但无法这样做。在下面的示例中,我取了最后三个值的平均值。我的代码只填充到 2017-01-10,而我想填充到 2017-01-14。对于 1/14,我想使用 11,12 和 13 的值。请帮忙。
import pandas as pd
df = pd.DataFrame([
{"ds":"2017-01-01","y":3},
{"ds":"2017-01-02","y":4},
{"ds":"2017-01-03","y":6},
{"ds":"2017-01-04","y":2},
{"ds":"2017-01-05","y":7},
{"ds":"2017-01-06","y":9},
{"ds":"2017-01-07","y":8},
{"ds":"2017-01-08","y":2},
{"ds":"2017-01-09"},
{"ds":"2017-01-10"},
{"ds":"2017-01-11"},
{"ds":"2017-01-12"},
{"ds":"2017-01-13"},
{"ds":"2017-01-14"}
])
df["y"].fillna(df["y"].rolling(3,min_periods=1).mean(),axis=0,inplace=True)
结果:
ds y
0 2017-01-01 3.0
1 2017-01-02 4.0
2 2017-01-03 6.0
3 2017-01-04 2.0
4 2017-01-05 7.0
5 2017-01-06 9.0
6 2017-01-07 8.0
7 2017-01-08 2.0
8 2017-01-09 5.0
9 2017-01-10 2.0
10 2017-01-11 NaN
11 2017-01-12 NaN
12 2017-01-13 NaN
13 2017-01-14 NaN
期望的输出:
最佳答案
您可以遍历 y 中的值,如果遇到 nan 值,请查看前 3 个值并使用 .at[] 将前 3 个值的平均值设置为新值:
for index, value in df['y'].items():
if np.isnan(value):
df['y'].at[index] = df['y'].iloc[index-3: index].mean()
缺失值的结果数据框:
7 2017-01-08 2.000000
8 2017-01-09 6.333333
9 2017-01-10 5.444444
10 2017-01-11 4.592593
11 2017-01-12 5.456790
12 2017-01-13 5.164609
13 2017-01-14 5.071331
关于python - Pandas fillna 和滚动平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64831334/
我有一个 Pandas 数据框 City State 0 Cambridge MA 1 NaN DC 2 Boston MA
我有一个 pandas 数据框,我想用 'colname_miss' 字符串填充缺失的类别。 def FillCatMissing(df): cols = ['A','B','C']
对于初学者,这里有一些符合我的问题的人工数据: df = pd.DataFrame(np.random.randint(0, 100, size=(vsize, 10)), col
fillna可以接受一个函数吗?或者只是“backfill”、“bfill”等方法? http://pandas.pydata.org/pandas-docs/stable/generated/pan
我想在多索引数据帧上使用 fillna,并仅在特定列位于同一索引中时填充该列。下一个索引可能以 n/a 开头,因此我无法在该列上调用 fillna 。组的大小正在变化,因此我无法通过数量来限制功能。
我正在尝试替换 pandas 数据框中的 NaT。 orders.PAID_AT 0 NaT 1 NaT 2
我有一个大型数据框,其值如下: Name A B C D E F G # Coulmns Matt 1 n n n 5 n 5 # rows Jake n n 2
我有一个大约 100 万行的大型数据集,大约有 5000 个缺失坐标(我想用类别“城市”的中值填充它们,但 fillna 正在工作,如何实现它? city = ['London', 'Paris',
我正在尝试用 if 条件做一个 fillna Fimport pandas as pd df = pd.DataFrame(data={'a':[1,None,3,None],'b':[4,None,
我有如下数据框 Re_MC,Fi_MC,Fin_id,Res_id, 1,2,3,4 ,7,6,11 11,,31,32 ,,35,38 df1 = pd.read_clipboard(sep
我有一个数据框,其中有一列连续但不相邻的数字和缺失值。 我想使用 fillna 函数用前一个非缺失行的增量值填充缺失值。 这是一个简化的表格: index my_counter 0 1 1
我有以下两个数据框 df1和 df2df1: A B C D 1 Nora NaN Japan 2 Neo NaN India 3
我试图填充所有缺失值,直到数据帧结束,但无法这样做。在下面的示例中,我取了最后三个值的平均值。我的代码只填充到 2017-01-10,而我想填充到 2017-01-14。对于 1/14,我想使用 11
我有一个关于给定主题的报告数据框。每份报告都有一个分数,受试者在某些日期有分数,但在其他日期没有分数。我想创建一个新的数据框,它只包含每个主题的最新分数。下面是一个 MRE。原始数据框如下所示:
我有一个如下所示的数据框 df = pd.DataFrame({'person_id': [101,101,101,101,202,202,202],
我正在处理一个汽车销售数据集,其中包含以下列:'car'、'price'、'body'、'mileage'、'engV'、'engType'、'registration'、'year'、'model'
我想估算 df['box_office_revenue'] 的缺失值中位数由 df['release_date'] == x 指定和df['genre'] == y 。 下面是我的中值查找函数。 de
我有一个带有“元数据”列的 Pandas 数据框,该列应包含字典作为值。但是,某些值丢失并设置为 NaN。我希望这是 {}。有时,整个列都丢失了,将其初始化为 {} 也是有问题的。 用于添加列 tsp
我正在尝试在特定条件适用的情况下用 0 填充某些行。我正在尝试: df.loc[:,(df.Available == True) & (df.Intensity.isnull())].Intensit
带有 NA 的 series_A 由一个 MultiIndex (X, Y) 索引,而要填写的值在 Series_B 中,它由 X 索引。如何有效解决此类问题? 例如,这里是series_A: bar
我是一名优秀的程序员,十分优秀!