Home > database >  How to make new dataframe within PANDAS by names within a column
How to make new dataframe within PANDAS by names within a column

Time:05-28

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

CodePudding user response:

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

CodePudding user response:

If you have a salesman called foo

new_df = df[df['salesmen'] == 'foo']

CodePudding user response:

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"]
  • Related