splitting into data frames

Viewed 31

Ok stackoverflow community, don't fail me now. I am wondering if there is a way to automate what I have just done. Essentially I took rows in my big dataframe and put them into smaller ones based on a condition in the taxonomy3 column. Then I took the log_price column and pulled that out to make a series so each one has their own dataframe. Is there a pandas command or anyother command for that matter that allows me to do this?:

#%%
data = pd.read_csv('../csv-files/interconnect.csv')
alt.data_transformers.enable('json')

#%%
# create tests
data_co = data.loc[data['taxonomy3'] == 'Connector Other']
data_colog = data_co['log_price']

data_ca = data.loc[data['taxonomy3'] == 'Cables']
data_calog = data_ca['log_price']

data_wpc = data.loc[data['taxonomy3'] == 'Wire and PCB Connectors']
data_wpclog = data_wpc['log_price']

data_CA = data.loc[data['taxonomy3'] == 'Connector Accessories']
data_CAlog = data_CA['log_price']

data_cm = data.loc[data['taxonomy3'] == 'Cable Management']
data_cmlog = data_cm['log_price']

data_io = data.loc[data['taxonomy3'] == 'I/O Connectors']
data_iolog = data_io['log_price']

data_cas = data.loc[data['taxonomy3'] == 'Cable Assemblies']
data_caslog = data_cas['log_price']
1 Answers

Create a list of values you want to look up and use dictionary comprehension

vals = ['Connector Other', 'Cables', 'Wire and PCB Connectors',
        'Connector Accessories', 'Cable Management', 'I/O Connectors',
        'Cable Assemblies']

dfs = {val: data[data['taxonomy3'] == val]['log_price'] for val in vals}

You can can then call each filtered DataFrame by using the string in vals

dfs['Connector Other'] # this will return all the "Connector Other"
dfs['Cables'] # this will return all the "Cables"
Related