create sets of similar dataframes from one dataframe

Question:

i have a dataframe that looks like this

name   x      y      z   ...
a      test   True   0
b      set    False  0
c      set    True   1
b      fix    True   1
c      set    False  0

i wanted to create multiple set of dfs with diff conditions

first df – for name = "…" that also satisfies the condition x = set

second df – for name = "…" that also satisfies the condition y = True

third df – for name = "…" that also satisfies the condition z = 1

ideally to have three df’s for specific names

edit: i’m sorry, i should’ve mentioned it before. i know i could subset and copy paste the commands, but i wanted to repeat this for 6-7 names and wanted something more efficient.

Asked By: anonymous_horse

||

Answers:

result = {}
for name in df.name.unique():
    name_df = df[df.name == name]
    result[name] = [
        name_df[name_df.x == 'set'],
        name_df[name_df.y],
        name_df[name_df.z == 1]
    ]
Answered By: Alex Bochkarev
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.