Answers for "create new dataframe from existing dataframe based on unique column values"

4

dataframe unique values in each column

for col in df:
    print(df[col].unique())
Posted by: Guest on August-20-2020
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 dataframe from existing dataframe based on unique column values"

Python Answers by Framework

Browse Popular Code Answers by Language