Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select subsets to show #283

Open
nanel96 opened this issue Aug 13, 2024 · 2 comments
Open

Select subsets to show #283

nanel96 opened this issue Aug 13, 2024 · 2 comments

Comments

@nanel96
Copy link

nanel96 commented Aug 13, 2024

Is there a way to only show certain subsets? The only method I see currently is to set a minimum or maximum size, however this is not very flexible. I like the ability to highlight certain subsets, but it would be more useful for some very large subplots to show only certain subsets of interest. Thanks.

@411an13
Copy link

411an13 commented Oct 15, 2024

Also looking for an answer to this question! Bumping it.

@411an13
Copy link

411an13 commented Oct 17, 2024

@nanel96 I made a basic function to select specific subsets to plot. Just feed it the dataframe you'd normally use to make the plot and a list of lists where each sublist corresponds to a subset you want to be included in the plot.

def slice_dataframe_by_index_combinations(df, combinations):
    """
    Slices a MultiIndex DataFrame based on index combinations where the values are `True`.
    All other index levels must have `False` values in the selected rows.

    Parameters:
    - df: A MultiIndex DataFrame.
    - combinations: A list of lists, where each sublist contains the index names 
                    that should be `True` in the desired slice.

    Returns:
    - A subset of the DataFrame where only rows with `True` for the specified index combinations are included.
    """
    # Create a list to hold the conditions for each combination
    conditions = []
    
    # Iterate through the combinations and build a condition for each one
    for combo in combinations:
        # Create a mask for the current combination
        mask = pd.Series(True, index=df.index)
        
        # Go through each level of the MultiIndex and apply the True/False condition
        for level in df.index.names:
            if level in combo:
                mask &= df.index.get_level_values(level) == True
            else:
                mask &= df.index.get_level_values(level) == False

        # Add this condition to the list of conditions
        conditions.append(mask)
    
    # Combine all conditions using the logical OR operator
    final_condition = conditions[0]
    for condition in conditions[1:]:
        final_condition |= condition
    
    # Return the filtered DataFrame
    return df[final_condition]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants