How to replace column values with NaN based on index with pandas

Question:

I have a data frame with multiple columns, the index is in a Time Stamp format. I want to locate a range of rows within a specific column based on their index and replace them with NaN. I think I need to combine the .loc and .replace functions to do this.

Example Input, dataframe with time stamp index and three columns :

Index                     'A'  'B' 'C'  
2023-02-03 10:00:00+00:00 0.1, 7, 8  
2023-02-03 11:00:00+00:00 6, 5.6, 3.2   
2023-02-03 12:00:00+00:00 9.5, 1.2, 6.3  
2023-02-03 13:00:00+00:00 -0.2, 1.1, 4.2  
2023-02-03 14:00:00+00:00 1.4, 7, 6.5  
2023-02-03 15:00:00+00:00 2.6, -6, 4  

Desired Output:

Index                     'A'  'B' 'C'  
2023-02-03 10:00:00+00:00 0.1, 7, 8  
2023-02-03 11:00:00+00:00 6, 5.6, 3.2   
2023-02-03 12:00:00+00:00 9.5, 1.2, 6.3  
2023-02-03 13:00:00+00:00 -0.2, NaN, 4.2  
2023-02-03 14:00:00+00:00 1.4, NaN, 6.5  
2023-02-03 15:00:00+00:00 2.6, NaN, 4  

The code:

df2=df.replace(df.loc['2023-02-03 13:00:00+00:00':df.index[-1],'B'],np.NaN)

Doesn’t give an error, but it doesn’t work either: output df2 is identical to df

Thanks!

Asked By: J.H

||

Answers:

Don’t replace, directly assign:

df2 = df.copy() # if needed to keep original

df2.loc['2023-02-03 13:00:00+00:00':df2.index[-1], 'B'] = float('nan')

df2:

                       Index    A    B    C
0  2023-02-03 10:00:00+00:00  0.1  7.0  8.0
1  2023-02-03 11:00:00+00:00  6.0  5.6  3.2
2  2023-02-03 12:00:00+00:00  9.5  1.2  6.3
3  2023-02-03 13:00:00+00:00 -0.2  NaN  4.2
4  2023-02-03 14:00:00+00:00  1.4  NaN  6.5
5  2023-02-03 15:00:00+00:00  2.6  NaN  4.0
Answered By: mozway
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.