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

Pandas capture connected rows

I have a following looking dataframe, e.g.:

ID      Value
0x3000  nan
nan     1
nan     2
nan     3
0x4252  nan
nan     10
nan     12

now, I’m looking for a way to get these two groups out of this dataframe, like so:

ID      Value
0x3000  nan
nan     1
nan     2
nan     3

and

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

ID      Value
0x4252  nan
nan     10
nan     12

so, a group basically starts on a hex value and contains its connected values all the way until the next occurence of valid hex value.

How can this be done effectively in pandas without manually looping through the rows and collecting row by row, until the condition (valid hex value) is met?

>Solution :

You can use groupby with a custom group to generate a list of DataFrames:

l = [g for _,g in df.groupby(df['ID'].notna().cumsum())]

output:

[       ID  Value
 0  0x3000    NaN
 1     NaN    1.0
 2     NaN    2.0
 3     NaN    3.0,
        ID  Value
 4  0x4252    NaN
 5     NaN   10.0
 6     NaN   12.0]
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