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
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?
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())]
[ 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]
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module