Python version: 3.7.3
Something similar was asked here, but it's not quite the same.
Based on a condition, I would like to retrieve only a subset of each group of the DataFrameGroupBy object. Basically, if a DataFrame starts with rows with only NANs, I want to delete those. If it isn't the case, I want the entire DataFrame to keep intact. To accomplish this, I wrote a function delete_rows
.
Grouped_object = df.groupby(['col1', 'col2'])
def delete_rows(group):
pos_min_notna = group[group['cumsum'].notna()].index[0]
return group[pos_min_notna:]
new_df = Grouped_object.apply(delete_rows)
However, this function seems to only do the "job" for the first group in the DataFrameGroupBy
object. What am I missing, so it does this for all the groups and "glues" the subsets together?
Function delete_rows
edited according to logic as provided by Laurens Koppenol