0

I have a dataframe which looks like

df = pd.DataFrame({'Fruits':['Apple','Avacado','Banana','Bluberry'],'Alphabet':['A','A','B','B']})

I want to subset this data frame based on the alphabet and name the subsets with the corresponding alphabet

The straight forward way of doing this is

A = df.loc[df['Alphabet'] == 'A']
B = df.loc[df['Alphabet'] == 'B']

But I would like to automate it, if in case I have all the 26 alphabets in the dataframe.

I tried putting the unique values of alphabets in to a list

alphabets = list(df['Alphabet'].unique())

And then created a list of individual dataframes

dfs = []
for letters in alphabets:
    df = df.loc[df['Alphabet']==letters]
    dfs.append(df)

Is it possible to assign the item names of the list alphabets to dataframe items of list dfs

Ahamed Moosa
  • 1,395
  • 7
  • 16
  • 30

0 Answers0