Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change multiple values in pandas df column to np.nan, based on condition in other column?

I don't have much experience in coding and this is my first question, so please be patient with me. I need to find a way to change multiple values of a pandas df column to np.nan, based on a condition in another column. Therefore I have created copies of the required columns "Vorgabe" and "Temp".

Whenever the value in "Grad" isn't 0 i want to change the values in a definded area in "Vorgabe" and "Temp" to np.nan.

print(df)  

    OptOpTemp  OpTemp  BSP  Grad  Vorgabe  Temp
0        22.0    20.0    5   0.0     22.0  20.0
1        22.0    20.5    7   0.0     22.0  20.5
2        22.0    21.0    8   1.0     22.0  21.0
3        22.0    21.0    6   0.0     22.0  21.0
4        22.0    23.5    7   0.0     22.0  20.0
5        23.0    21.5    1   0.0     23.0  21.5
6        24.0    22.5    3   1.0     24.0  22.5
7        24.0    23.0    4   0.0     24.0  23.0
8        24.0    25.5    9   0.0     24.0  25.5

So I want to achieve something like this:

    OptOpTemp  OpTemp  BSP  Grad  Vorgabe  Temp
0        22.0    20.0    5   0.0     22.0  20.0
1        22.0    20.5    7   0.0     nan   nan      <-one row above
2        22.0    21.0    8   1.0     nan   nan
3        22.0    21.0    6   0.0     nan   nan      <-one row among
4        22.0    23.5    7   0.0     22.0  20.0
5        23.0    21.5    1   0.0     nan   nan
6        24.0    22.5    3   1.0     nan   nan
7        24.0    23.0    4   0.0     nan   nan
8        24.0    25.5    9   0.0     24.0  25.5

Does somebody have a solution to my problem?

EDIT: I may have been unclear. The goal is to change every value in "Vorgabe" and "Temp" in an defined area to nan. In my example the area would be one row above, the row with 1.0 in it, and one row among. So not only the row, where 1.0 is located, but also rows above and under.

like image 643
Jeddy Eastside Avatar asked Jan 01 '23 11:01

Jeddy Eastside


2 Answers

Use loc:

df.loc[df.Grad != 0.0, ['Vorgabe', 'Temp']] = np.nan
print(df)

Output

   OptOpTemp  OpTemp  BSP  Grad  Vorgabe  Temp
0       22.0    20.0    5   0.0     22.0  20.0
1       22.0    20.5    7   0.0     22.0  20.5
2       22.0    21.0    8   1.0      NaN   NaN
3       22.0    21.0    6   0.0     22.0  21.0
4       22.0    23.5    7   0.0     22.0  20.0
5       23.0    21.5    1   0.0     23.0  21.5
6       24.0    22.5    3   1.0      NaN   NaN
7       24.0    23.0    4   0.0     24.0  23.0
8       24.0    25.5    9   0.0     24.0  25.5
like image 99
Dani Mesejo Avatar answered Feb 01 '23 22:02

Dani Mesejo


You could use numpy.where.

import numpy as np

df['Vorbage']=np.where(df['Grad']!=0, df['OptOpTemp'], np.nan)
df['Temp']=np.where(df['Grad']!=0, df['OpTemp'], np.nan)
like image 24
Ricky Kim Avatar answered Feb 02 '23 00:02

Ricky Kim