简体   繁体   English

将 nan 值水平填充到 dataframe

[英]Horizontally filling nan values into dataframe

I have a large dataframe, with the data set up as such:我有一个大的 dataframe,数据设置如下:

df = [
[0, 1, 2, nan, nan, 5, nan, nan],
[nan, 1, 2, 3, nan, nan, 6, nan],
[nan, nan, 3, 4, nan, 6, nan, nan]
 ]

Expected Output: 
df=[
[0, 1, 2, nan, nan, nan, nan, nan], 
[nan, 1, 2, 3, nan, nan, nan, nan],
[nan, nan, 3, 4, nan, 6, nan, nan]
]

I am trying to figure out an apply function by row that remove the values and replace them with a nan if numerical values have occurred, then a nan value, essentially removeing the 5 and 6 values in the data.我试图找出一个应用 function 按行删除值并在出现数值时用 nan 替换它们,然后是 nan 值,基本上删除数据中的 5 和 6 值。

Thanks!谢谢!

It's hard to understand your meaning, but if you're just trying to replace a given value with nan, you can use np.where很难理解你的意思,但如果你只是想用 nan 替换给定的值,你可以使用 np.where

df = pd.DataFrame(np.where(df==5, np.nan, df))

I fixed this by looping through columns (know I thought I wanted to do a row loop but this worked).我通过遍历列来解决这个问题(知道我以为我想做一个行循环,但这很有效)。 I determined this by looking at all previous rows, determining if there was a nan, a numerical, then a nan, and if the current column was numerical.我通过查看所有先前的行来确定这一点,确定是否有一个 nan、一个数字、一个 nan,以及当前列是否是数字。

Thanks for all the help!感谢所有的帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM