Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace all the non-first values of columns with NaN based on date?

I have this dataframe:

df

I want to replace the non-First values of the columns with NaN, for each day.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is how should the dataframe look like:

df desidered

This is what i tried:

import pandas as pd
from datetime import datetime
tbl = {"date" :["2022-02-27", "2022-02-27", "2022-02-27", "2022-02-27","2022-02-27", "2022-02- 
                28", "2022-02-28","2022-02-28", "2022-02-28"],
      "value1" : ["NaN", 0.1, 0.1, "NaN", "NaN", "NaN", "NaN", 0.3, "NaN"],
      "value2" : ["NaN", "NaN", 0.2, 0.3, "NaN", 0.3, 0.4, "NaN", "NaN"]}


df = pd.DataFrame(tbl)
df = df.replace('NaN', float('nan'))
pd.to_datetime(df['date'], format='%Y-%m-%d')

#i’m trying to use replace, but this does not consider the date

DataFrame.replace(to_replace=None, value=NoDefault.no_default, inplace=False, limit=None, 
regex=False, method=NoDefault.no_default)

>Solution :

groupby + rank

First create boolean mask with isna, then use groupby + rank with method='first' to assign numerical ranks, finally mask the values in the original dataframe where rank is 1

df = df.set_index('date')
df[df.isna().groupby('date').rank(method='first').eq(1)]

Result

            value1  value2
date                      
2022-02-27     NaN     NaN
2022-02-27     0.1     NaN
2022-02-27     NaN     0.2
2022-02-27     NaN     NaN
2022-02-27     NaN     NaN
2022-02-28     NaN     0.3
2022-02-28     NaN     NaN
2022-02-28     0.3     NaN
2022-02-28     NaN     NaN
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading