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

create sets of similar dataframes from one dataframe

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

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

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.

>Solution :

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]
    ]
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