1

I have a dataframe like:

df
    name value
0   a    10
1   a    5
2   b    4
3   b    8

I want to create filtered datasets using the 'name' feature. Something like:

for i in ['a', 'b']:
    df_{i} = df[df['name'] == i]

df_a
    name value
0   a    10
1   a    5

df_b
    name value
2   b    4
3   b    8

Which alterations are necessary to do that?

0

2 Answers 2

1

You can use unique() and save those in a dict() (fdf):

import pandas as pd


data = {
    'name': ['a', 'a', 'b', 'b'],
    'value': [10, 5, 4, 8]
}
df = pd.DataFrame(data)

fdf = {}
for i in df['name'].unique():
    fdf[f'df_{i}'] = df[df['name'] == i]

df_a, df_b = fdf['df_a'], fdf['df_b']

print("df_a:")
print(df_a)
print("\ndf_b:")
print(df_b)

Prints


df_a:
  name  value
0    a     10
1    a      5

df_b:
  name  value
2    b      4
3    b      8
Sign up to request clarification or add additional context in comments.

Comments

0

Code

Creating variables dynamically is not recommended in Python.

Create dict for that purpose.

  1. dictionary comprehension

     dict_grouped = {k: d for k, d in df.groupby('name')}
    

    dict_grouped:

     {'a':   name  value
      0    a     10
      1    a      5,
      'b':   name  value
      2    b      4
      3    b      8}
    

  1. dict func

    Alternatively, you can also use the following code instead of dictionary comprehension.

     dict_grouped = dict(tuple(df.groupby('name')))
    


If you absolutely must create it dynamically as a variable, you can use the code below, but i don't recommend it.

for k, d in df.groupby('name'):
    globals()[f'df_{k}'] = d

df_a:

    name    value
0   a       10
1   a       5

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.