无法分配“ nan”/np中的空价值。

发布于 2025-02-05 18:53:35 字数 319 浏览 3 评论 0原文

我正在尝试将我的“ np。”条件中的空单元(空白/nan)分配给我的熊猫数据框架,但似乎没有任何用途。

这样做的原因是要运行fillna,ffill在缺失值上。

np.Where代码:

df['x'] = np.where(df['y']>0.05,1,np.nan)

填充代码:

df['x'] = df['x'].fillna(method="ffill")

有人知道我要去哪里了吗?

I'm trying to assign an empty cell (blank/nan) in my "np.where" condition to my pandas dataframe, but nothing seems to work.

The reason for this is to run fillna,ffill on the missing values.

Np.Where code:

df['x'] = np.where(df['y']>0.05,1,np.nan)

Fillna code:

df['x'] = df['x'].fillna(method="ffill")

Anybody know where I'm going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温暖的光 2025-02-12 18:53:35

我能够使用pandas.na而不是fillna 来修复

它:

df['x'] = np.where(df['y']>0.05,1,pd.NA)
df['x'] = df['x'].fillna(method="ffill")

I was able to fix it using pandas.NA instead which fillna, for some reason, recognizes as blanks to fill with ffill

Fix:

df['x'] = np.where(df['y']>0.05,1,pd.NA)
df['x'] = df['x'].fillna(method="ffill")
沫雨熙 2025-02-12 18:53:35

这条代码有效:
df ['x'] = np.Where(df ['y']> 0.05,1,np.nan)
只需删除右边的不需要的parathess

this line of code works:
df['x'] = np.where(df['y']>0.05,1,np.nan)
just remove the unneeded paratheses in the right

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文