I want to filter values across multiple columns creating dataframes for the unique value combinations. Any help would be appreciated.
Here is my code that is failing (given dataframe df):
dd = defaultdict(dict) #create blank default dictionary
values_col1 = df.col1.unique() #get the unique values from column 1 of df
for i in values_col1:
dd[i] = df[(df['col1']==i)] #for each unique value create a sorted df and put in in a dictionary
values_col2 = dd[i].col2.unique() #get the unique values from column2 of df
for m in values_col2:
dd[i][m] = dd[i][(dd[i]['col2']==m)] #for each unique column2 create a sub dictionary
When I run it I get a very long error message. I won't insert the whole thing here, but here is some of it:
C:\Anaconda3\lib\site-packages\pandas\indexes\base.py in get_loc(self, key, method, tolerance) 1944 try: -> 1945 return self._engine.get_loc(key) 1946 except KeyError:
...
ValueError: Wrong number of items passed 6, placement implies 1