- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我们正在尝试通过列表重新索引数据帧,如下所示:
dataframe.reindex(list)
其中确切的数据框和列表并不重要。
现在,如果列表大于原始索引,就会出现一些 NaN。我的问题是,如果我想通过前向填充来填充 NaN,以下两种方法有什么区别:
dataframe.reindex(list, method='ffill')
dataframe.reindex(list).ffill()
我尝试了几次,发现结果不同。
假设我们的数据框是:
Jan 1
Apr 2
Jul 3
Oct 4
列表是:
列表 = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月' ]
所以,
dataframe.reindex(list, method='ffill')
变成:
Jan 1
Feb 2
Mar 3
Apr 2
May 3
Jun 3
Jul 3
Aug 2
Sep 4
Oct 4
Nov 3
Dec 2
鉴于,
dataframe.reindex(list).ffill()
变成:
Jan 1
Feb 1
Mar 1
Apr 2
May 2
Jun 2
Jul 3
Aug 3
Sep 3
Oct 4
Nov 4
Dec 4
我不明白这两种方式有何不同?第一种方式会产生什么,第二种方式会产生什么?
我尝试查找文档和其他来源,但找不到解释。预先非常感谢。
最佳答案
编辑:
在 pandas 0.24.1 版本中引发错误:
print (dataframe)
Col
Jan 1
Apr 2
Jul 3
Oct 4
print (dataframe.index.is_monotonic_increasing)
False
L = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
print (dataframe.reindex(L, method='ffill'))
ValueError: index must be monotonic increasing or decreasing
在文档中 - DataFrame.reindex
:
method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}
Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index.None (default): don't fill gaps pad / ffill: propagate last valid observation forward to next valid backfill / bfill: use next valid observation to fill gap nearest: use nearest valid observations to fill gap
第二次工作很好:
print (dataframe.reindex(L).ffill())
Col
Jan 1.0
Feb 1.0
Mar 1.0
Apr 2.0
May 2.0
Jun 2.0
Jul 3.0
Aug 3.0
Sep 3.0
Oct 4.0
Nov 4.0
Dec 4.0
<小时/>
注意 - 不要使用 list
之类的变量,因为 python 保留字(内置)。
区别在于在重新索引
之前列中是否存在缺失值:
dataframe = pd.DataFrame({'col':[0,2,np.nan,5,np.nan]}, index=[1,3,5,6,8])
print (dataframe)
col
1 0.0
3 2.0
5 NaN
6 5.0
8 NaN
print (dataframe.index.is_monotonic_increasing)
True
如果使用参数,则仅前向填充添加的行,此处为 1,4,7 行。
如果链ffill
则创建缺失值并且Series的所有数据都是前向填充:
L = range(10)
df = pd.concat([dataframe.reindex(L, method='ffill'),
dataframe.reindex(L),
dataframe.reindex(L).ffill()],
keys=('parameter','only_reindex','chained'), axis=1)
print (df)
parameter only_reindex chained
col col col
0 NaN NaN NaN
1 0.0 0.0 0.0
2 0.0 NaN 0.0
3 2.0 2.0 2.0
4 2.0 NaN 2.0
5 NaN NaN 2.0
6 5.0 5.0 5.0
7 5.0 NaN 5.0
8 NaN NaN 5.0
9 NaN NaN 5.0
<小时/>
如果没有缺失值结果相同:
dataframe = pd.DataFrame({'col':[0,2,8,5,9]}, index=[1,3,5,6,8])
print (dataframe)
col
1 0
3 2
5 8
6 5
8 9
print (dataframe.index.is_monotonic_increasing)
True
L = range(10)
df = pd.concat([dataframe.reindex(L, method='ffill'),
dataframe.reindex(L),
dataframe.reindex(L).ffill()],
keys=('parameter','only_reindex','chained'), axis=1)
print (df)
parameter only_reindex chained
col col col
0 NaN NaN NaN
1 0.0 0.0 0.0
2 0.0 NaN 0.0
3 2.0 2.0 2.0
4 2.0 NaN 2.0
5 8.0 8.0 8.0
6 5.0 5.0 5.0
7 5.0 NaN 5.0
8 9.0 9.0 9.0
9 9.0 NaN 9.0
关于python - python 中的 ffill 作为链式方法和作为参数有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54703868/
一晃五年没写博客了,依旧再C#上耕耘,依旧没有啥建树,现在也不知道.net上还有多少人再使用,在这里分享一些自己觉得写的还算优雅的代码。 对于自己写着完的代码,我特别喜欢链式(来源于jQuer
我正在构建一个吉他和弦查找应用程序。我使用多维数组来表示指板。数组中的每个元素都由具有字符串属性“Note”的 FretSpace 结构表示。为了初始化指板上的音符属性,我传递了要处理的吉他弦的详细信
我在演示代码中使用 setTimeout 函数模拟了 3 个 ajax 调用。我将从一段运行良好的代码开始:所有调用都是并行进行的,我希望所有调用都能成功,否则会出现错误。 var p1 = func
谁能解释一下? a = [2,3,4] b = [5,6,8,9] print(len(a) > 0) print(len(b) > 0) print((len(a) > 0) & len(b) >
我正在处理具有多个子 JSONObject 的 JSONObject。这是我填写内容的方式: myJson.getJSONObject(CAT_NAME).put(VAR_NAME, var)
想象一下这种情况,我有一个需要检查属性的对象。但是,该对象当前可以具有空值。 如何在一个“if”条件下检查这两个条件? 目前,我必须做这样的事情: if (myObject != null) {
我有一个对象集合,称它们为obj。他们有一个 act() 方法。 act() 方法最终会导致 o 上的 event() observable 调用 onComplete。 链接这些的好方法是什么? 即
假设我有一个列表变量 datalist 存储 10,000 个字符串实体。QTableView 只需要显示其中的一些实体。这就是为什么 QTableView 被指定为 QSortFilterProxy
我正在寻找支持链式 MSI 安装的工具(最好不是 InstallShield,而且最好是便宜/免费的)。我有几个小型安装需要能够单独部署,但也需要作为一个组部署,我不想维护多个安装程序。 看起来我需要
在这种情况下,我想迭代集合中除最后 2 个元素之外的所有元素。 假设我采用了一种奇怪的方式,例如 x.Reverse().Skip(2).Reverse()。 每个 LINQ 操作是否会有效地生成一个
对于javascript来说非常陌生,我有两个html数字选择,包括年份,我想将第二个选择与第一个选择链接起来,这样当我在第一个选择中选择年份时(而第二个选择没有选项)首先),第二个选择应包括从所选数
有人可以向我解释一下为什么以下两个链式函数: // returns zero if okay var resetCounter = function (model) { return new Prom
所以我有 2 个 promise 函数。当第一个函数出现错误时,我希望它显示错误消息。当完成或失败时,我希望他们执行一个finally catch all 函数,但由于某种原因它不起作用。我的代码如下
我有一个函数 const func = () => server.insertPatientSurveyQuestionToDataBase(Store.getPatientID(), SurveyN
(async function() { var a,b; function flush(){ return new Promise(res => {
这个问题已经有答案了: Promise chaining: Use result from previous promise in next then callback [duplicate] (1
这可能不是专业正则表达式理解的问题。唯一重要的是因为我正在运行多个链式替换命令,这些命令会影响文本文件中的某些相同文本。我还想象在替换之前,根据分隔符词(需要多次替换)的使用方式对 txt 文件进行分
我正在尝试构建一组类来定义 OSI 堆栈中协议(protocol)的分层属性...从抽象意义上讲,我只需要从父 python 类继承属性,但我需要能够调用整个类链一次...所以,我正在寻找这样的东西.
我正在努力兑现 promise ,到目前为止我偶然发现了这一点: new Promise((resolve, reject) => { setTimeout(() => { r
我试图理解 promise ,我需要链接它们并装饰来自不同端点的对象宽度数据。 例如: 我的 Node-express 应用程序中有这个 //controller.js export const ge
我是一名优秀的程序员,十分优秀!