简体   繁体   English

如果一个值是 NaN,Pandas 用 NaN 替换一行中的所有项目

[英]Pandas replace all items in a row with NaN if one value is NaN

I want to get rid of some records with NaNs.我想用 NaN 去掉一些记录。 This works perfectly:这完美地工作:

df.dropna(axis=0, how='any',inplace=True)

However, it changes the shape of my dataframe, and the index is no longer uniformly spaced.但是,它改变了我的数据框的形状,并且索引不再均匀间隔。 Therefore, I'd like to replace all items in these rows with np.nan .因此,我想用np.nan替换这些行中的所有项目。 Is there a simple way to do this?有没有一种简单的方法可以做到这一点?

I was thinking about resampling the dataframe after dropna , but that only seems to work with a prescribed interval, whereas I would rather use the original index.我正在考虑在dropna之后重新采样数据帧,但这似乎只适用于规定的间隔,而我宁愿使用原始索引。 Another approach would be to loop over the dataframe with iterrows , but that also feels cumbersome.另一种方法是使用iterrows循环遍历数据帧,但这也感觉很麻烦。

下面的命令选择具有等于 Nan 的任何值的所有行,并将 NaN 分配给这些行的其余部分。

df.loc[df.isnull().any(axis=1), :] = np.nan

使用此代码也无需切片df = df.replace('nan',np.nan)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果一个值是NaN,则熊猫用NaN替换一行中的所有值 - Pandas replace all values in a row with NaN if one value is NaN 如果 Nan 在单独的行中,则将值替换为 Nan - Pandas - Replace value with Nan if Nan in separate row - Pandas 如何使用 Pandas 根据同一行中另一列的值替换一列中的 NaN 值? - How to replace NaN value in one column based on the value of another column in the same row using Pandas? 如何用 NaN 替换 pandas 中的值? - How to replace a value in pandas, with NaN? 如果特定列的一个值为 NaN,有没有办法使用 ffill 替换整个 pandas dataframe 行? - Is there a way to replace a whole pandas dataframe row using ffill, if one value of a specific column is NaN? Python Pandas 用另一列下一行的值替换一列中的 NaN - Python Pandas replace NaN in one column with value from a row below of another column Python Pandas 将一列中的 NaN 替换为与列表列相同行的另一列中的值 - Python Pandas replace NaN in one column with value from another column of the same row it has be as list column Python Pandas 用第二列对应行的值替换一列中的 NaN - Python Pandas replace NaN in one column with value from corresponding row of second column 当列值匹配时,Pandas Dataframe会从行中替换Nan - Pandas Dataframe replace Nan from a row when a column value matches 熊猫数据框:用该行的均值替换nan - Pandas data frame: replace nan with the mean of that row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM