0

'''df.sort_values(by=['salesmen'])'''

If I used this to sort my data by salesmen names within a column how can I then automatically create a new dataframe for each salesmen within pandas?? Thank you

max2lax
  • 5
  • 2
  • Does this answer your question? [Pandas split DataFrame by column value](https://stackoverflow.com/questions/33742588/pandas-split-dataframe-by-column-value) – C8H10N4O2 May 27 '22 at 21:09

3 Answers3

0

Perhaps by using pd.DataFrame.where() you can create a condition to capture each salesmen and save the output to a new dataframe? https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.where.html

Andrew W
  • 1
  • 1
  • 1
0

If you have a salesman called foo

new_df = df[df['salesmen'] == 'foo']
Nohman
  • 444
  • 2
  • 9
0

the below code should work nicely if you would like to have have separate dfs per salesman.

import pandas as pd

df = pd.DataFrame({
    'salesmen':['maria', 'carlos', 'andrea'],
    'id':[1, 2, 3]
    }
)
df = df.sort_values(by=['salesmen'])
df = dict(tuple(df.groupby('salesmen')))

#example
df["andrea"]


# df["maria"]
# df["carlos"]
fiddy
  • 76
  • 4