def partition_df(df, col): # return dictionary object of filtered data frame for each unique element in col try: # dict comprehension creates dict to holds DataFrame for each country dict1 = {k: df[df[col] == k] for k in m.get_unique_vals(df, col)} return dict1 except (TypeError): print(TypeError('arg1 must be a DataFrame')) except KeyError as e: cause = e.args[0] print(cause + ' not a valid column in the dataframe')
def partition_df( df, col ): # return dictionary object of filtered data frame for each unique element in col try: # dict comprehension creates dict to holds DataFrame for each country dict1 = {k: df[df[col] == k] for k in m.get_unique_vals(df, col)} return dict1 except (TypeError): print(TypeError('arg1 must be a DataFrame')) except KeyError as e: cause = e.args[0] print(cause + ' not a valid column in the dataframe')
def test_returns_only_unique_vals(self): self.assertEquals(len(v.filter_by_col_val(df, 'pop', 0)), 2)
def test_if_col_not_exist_keyerror(self): self.assertRaises(KeyError, v.filter_by_col_val(df, 'qwerty1', 'qwerty2'))
def test_amount_needed_calculation_correct(self): new_frame = v.add_amount_needed(df, 'goal', 'raised') amount_n = new_frame['amount_needed'] self.assertEquals(amount_n[0], 2) self.assertEquals(amount_n[5], 0)
def test_if_col_not_exist_keyerror(self): self.assertRaises(KeyError, v.add_amount_needed(df, 'qwerty1', 'qwerty2'))
def test_returns_only_unique_vals(self): self.assertEquals(len(v.get_unique_vals(df, 'country')), 5) self.assertEquals(len(v.get_unique_vals(df, 'pop')), 2)
def test_if_col_not_exist_keyerror(self): self.assertRaises(KeyError, v.get_unique_vals(df, 'qwerty1'))