felixpradoh
felixpradoh

Reputation: 145

Replace dataframe values by NaN

I am working with a pandas dataframe of 15 rows and 8 columns, such a:

              A          B  ...          G          H
0      0.158979   0.187282  ...   0.330566   0.458748    
1      0.227254   0.273307  ...   0.489372   0.649698    
2      0.308775   0.351285  ...   0.621399   0.833404    
3      0.375850   0.444228  ...   0.759206   0.929980    
4      0.431860   0.507906  ...   0.850741   1.038544    
5      0.507219   0.596291  ...   0.980404   1.145819    
6      0.570170   0.676551  ...   1.094201   1.282077    
7      0.635122   0.750434  ...   1.155645   1.292930    
8      0.704220   0.824748  ...   1.261516   1.395316    
9      0.762619   0.887669  ...   1.337860   1.410864    
10     0.824553   0.968889  ...   1.407665   1.437886    
11     0.893413   1.045289  ...   1.519902   1.514017    
12     0.946757   1.109964  ...   1.561611   1.478634    
13     1.008294   1.174139  ...   1.596135   1.501220    
14     1.053086   1.227203  ...   1.624630   1.503892

where columns from C to F have been omitted.

I would like to know how I can find the closest value to 1 for every column. Once this value is found I would like to replace the rest of the values in the columns by NaN, with the exception of the values corresponding to the previous and next row. Then obtaining a dataframe like that:

            A          B    ...          G          H
0           NaN        NaN  ...        NaN        NaN    
1           NaN        NaN  ...        NaN        NaN    
2           NaN        NaN  ...        NaN        NaN    
3           NaN        NaN  ...        NaN   0.929980    
4           NaN        NaN  ...   0.850741   1.038544    
5           NaN        NaN  ...   0.980404   1.145819    
6           NaN        NaN  ...   1.094201        NaN    
7           NaN        NaN  ...        NaN        NaN    
8           NaN        NaN  ...        NaN        NaN    
9           NaN   0.887669  ...        NaN        NaN    
10          NaN   0.968889  ...        NaN        NaN    
11          NaN   1.045289  ...        NaN        NaN    
12     0.946757        NaN  ...        NaN        NaN    
13     1.008294        NaN  ...        NaN        NaN    
14     1.053086        NaN  ...        NaN        NaN

Does anyone has a sugestion for this? Thanks in advance

Upvotes: 1

Views: 78

Answers (1)

Ben.T
Ben.T

Reputation: 29635

you can use the fact that the closest to 1 is actually the min of the abs of df once remove 1. So check where the min is meet, use shift once with 1 and once with -1 to get the next and previous row. use this mask in where.

df_ = (df-1).abs()
df_ = df_.min() == df_
df_ = df_|df_.shift(1)|df_.shift(-1)
df_ = df.where(df_)
print(df_)
           A         B         G         H
0        NaN       NaN       NaN       NaN
1        NaN       NaN       NaN       NaN
2        NaN       NaN       NaN       NaN
3        NaN       NaN       NaN  0.929980
4        NaN       NaN  0.850741  1.038544
5        NaN       NaN  0.980404  1.145819
6        NaN       NaN  1.094201       NaN
7        NaN       NaN       NaN       NaN
8        NaN       NaN       NaN       NaN
9        NaN  0.887669       NaN       NaN
10       NaN  0.968889       NaN       NaN
11       NaN  1.045289       NaN       NaN
12  0.946757       NaN       NaN       NaN
13  1.008294       NaN       NaN       NaN
14  1.053086       NaN       NaN       NaN

Upvotes: 2

Related Questions