问题标签 [fillna]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
1086 浏览

python - 更改熊猫的轴替换填充

假设我有一个看起来像这样的数据框:

然后可以使用df.fillna(method='ffill', axis=1)获得:

即我转发填充行。但是,现在我有一个数据框,-1而不是np.nan. Pandas 具有replace也可以使用的函数method='ffill',但replace()不接受轴参数,因此要获得与上述相同的结果,我需要调用df.T.replace(-1, method='ffill').T. 由于转置非常昂贵(特别是考虑到我正在处理数 GB 的数据帧),所以这不是一个选择。我怎样才能达到预期的结果?

0 投票
3 回答
2691 浏览

python - 熊猫:在数据框中重命名“未命名:*”或“NaN”

到目前为止,这是我的代码:

这是它的样子: 在此处输入图像描述

我想将“未命名:*”列重命名为最后一个有效名称。

这是我尝试过的方法和结果:

如果我这样做,这“有效”

但是我有空白值或 NaN(如果我用 'NaN' 替换''。然后我尝试:

这没有效果。所以我尝试了就地=真:

df.columns = df.columns.fillna('ffill', inplace=True)

然后我尝试了另一种方法:

这给了我这个错误:

0 投票
1 回答
6053 浏览

python - How does pandas replace NaN values with mean value using groupby

I tried using this to replace the NaN values in the column feature count ( its an integer that ranges from 1 to 10 ) using groupby ( client_id or client _ name ) , however the NaN values do not seem to go.

The output is :

Now I use:

But the output remains the same :

Any other way to replace the NaN values by the means of other non NaN values of the column grouped by their IDs?

0 投票
2 回答
1189 浏览

python - 带有衰减的 Pandas 数据帧前向填充

我正在运行 Python 3.5 和 Pandas v 0.19.2。我有一个如下所示的数据框。向前填充缺失值是直截了当的。

我的问题是:用衰减实现前向填充的最佳方法是什么?我理解pd.ffill()并且pd.fillna()不支持这个。例如,我所追求的输出如下(与上面的常规 ffill 相比),其中每个周期的值都减半:

0 投票
2 回答
1513 浏览

python - Pandas - 使用分类处理数据透视表中的 NaN

我正在使用Categoricaldtype 创建多个数据透视表,然后将它们合并到一个大数据透视表/数据框中。

但是,在某些情况下,我NaN在执行合并时得到,当我尝试时fillna(0),我得到以下错误:ValueError: fill value must be in categories

实际输出:

期望的输出:

0 投票
1 回答
35 浏览

python - filevalues.fillna 仅填充某些文件

我有彼此具有相同格式的销售预测文件。当我更改读取文件的值(现在我正在读取文件 42.csv)时,有时列会填充 0 以表示空值,有时它们不会

我不确定为什么会发生这种情况,因为所有文件都具有相同的格式并且看起来非常相同。关于为什么会发生这种情况的任何想法?如果需要我的文件的屏幕截图,请告诉我

另外,对于没有填充的文件,当我在没有fillvalues.fillana(0)函数的情况下运行这个程序时,没有填充0的文件仍然只显示空格,而填充0的文件做显示nAn。我想一个更好的问题是,对于这两个文件,它们都有空格,但是 python 似乎将其中一些检测为 nAn,而其中一些只是空白,并且没有在那里写任何东西。为什么??

0 投票
1 回答
444 浏览

python-3.x - 即使将替换就地设置为true,Pandas Dataframe fillna 的工作也不一致

程序从 RESTApi 检索 JSON 数据

CSV 数据包含一些字段的空值和少数字段的 NaN。

输入

在使用 fillna 将所有 Null 和 NaN 替换为 0 时,因为它们是目标表中的数字字段。

试过的代码:

输出

'立即录取','0','BLAHBLAH','0','NaN','0','0','0','0','0.0','0','0',' 0'

如何确保我的数据框中的所有 NaN 值都更改为 0,因为它们加载到的表由于 NaN 值而拒绝值?

我从 RESTAPI 逐行处理数据切换到 Dataframe,印象是使用 DF 更容易处理数据。如果fillna不起作用,是否有更好的方法来按摩df中的数据而无需逐行迭代?

更新:

我尝试在之前和之后编写 df.fillna 的输出。少数领域出现了部分变化,但并非所有领域都发生了变化

前:

后:

df.dtypes 输出

df.values[5:6, :15]

0 投票
0 回答
74 浏览

numpy - 更高效的fillna(numpy)

我需要一个类似Pandas.fillna的函数的数组版本,在论坛里收集了很多答案创建如下函数,但是还是比Pandas.fillna慢3倍,想知道有没有更好的优化方法,谢谢。

0 投票
1 回答
98 浏览

python - 如果其他列中的值相同,则向前或向后填充 NA

给定这个例子:

因为前三行具有相同的hidhospital,所以 中的值identity也应该相同。至于其他两行,它们具有相同的hidhospital,但没有提供已知identity信息,因此中的值identity应该仍然缺失。换句话说,期望的输出是:

我可以遍历hids 和hospitals like的所有组合for hid, hospital in df[["hid", "hospital"]].drop_duplicates().itertuples(index=False),但我不知道下一步该怎么做。

0 投票
0 回答
454 浏览

python - Pandas -- iterrows for fillna()

I have a large data frame of at least 500 rows and 60 columns. Let's call this df1. Some cells have NaN in them, which I've typically used

df2 = df1.fillna(value = 0.0)

in order to replace them in just one line of code.

But this is with much, much smaller dataframes, where it worked in the past, and I would like to implement the same step on larger volumes of data in the future. Using the above command goes above my recursion limit. I've also tried to increase the recursion limit, and my program would crash as a result.

My initial recursion limit is:

Then increasing it with

and running fillna() afterwards makes my program crash.

In order to work around recursion and rather make this command work iteratively, I created this loop:

But I get an error message that to my interpretation looks like it's telling me that an index (which is 711?) is out of bounds, and it's also interpreted as a KeyError:

Do I need to fix my loop or try something else?