Answers for "Create new data frames from existing data frame based on unique column values"

0

Create new data frames from existing data frame based on unique column values

import pandas as pd

df = pd.DataFrame({
    "company_id": ["AA", "AB", "AA", "CD", "AB"],
    "company_score": [.07, .08, .06, .0003, .09],
    "company_region": ["NW", "NE", "NW", "NW", "NE"]})

# Approach 1
dict_of_companies = {k: v for k, v in df.groupby('company_id')}

# Approach 2
dict_of_companies = dict(tuple(df.groupby("company_id")))

import pprint
pprint.pprint(dict_of_companies)
Posted by: Guest on April-17-2021

Code answers related to "Create new data frames from existing data frame based on unique column values"

Browse Popular Code Answers by Language