简体   繁体   中英

Horizontally filling nan values into dataframe

I have a large dataframe, with the data set up as such:

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.

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

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.

Thanks for all the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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