0

Can anyone tell me how to create a list of data frames from a data frame based on unique values of a column in the data frame in python

Sairam Ch
  • 41
  • 6

1 Answers1

1

Assume, your column is named yourcol and the data is stored in df. Use df.query:

listofdfs = [df.query('yourcol={}'.format(val)) for val in df[yourcol].unique()]

Or use loc:

listofdfs = [df.loc[df['yourcol']==val, :] for val in df[yourcol].unique()]

Other option would be groupby.

Have a look on the answers here, it was already answered.

Nico Albers
  • 1,556
  • 1
  • 15
  • 32