7 examples of 'convert groupby to dataframe pandas' in Python

Every line of 'convert groupby to dataframe pandas' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
38def add_group_id(df, *groupby_cols, gid_colname='gid'):
39 groupby_cols = list(groupby_cols)
40 df_group = df.groupby(groupby_cols).apply(lambda g: pd.Series({
41 'group_length': g.shape[0]
42 })).reset_index()
43 df_group[gid_colname] = df_group.index
44 df_merge = pd.merge(df, df_group, how='outer', on=groupby_cols)
45 df_merge['group_length'] = df_merge['group_length'].fillna(-1)
46 df_merge[gid_colname] = df_merge[gid_colname].fillna(-1)
47 df_merge['group_length'] = df_merge['group_length'].astype(int)
48 df_merge[gid_colname] = df_merge[gid_colname].astype(int)
49 return df_merge
59def to_pandas(self):
60 """Encode aggregation result as a pandas dataframe.
61
62 Returns:
63 Pandas dataframe with aggregation results.
64 """
65 return pandas.DataFrame(self.values)
317def test_groupby_select_all_columns():
318 # Check that when selecting all columns, the result has the same number
319 # of columns as the original.
320 DT = dt.Frame(id2=[1, 2] * 3, id4=[1] * 6, v3=[1, 3, 2, 3, 3, 3])
321 res = DT[:, :, by(f.id2, f.id4)]
322 assert_equals(res, dt.Frame(id2=[1, 1, 1, 2, 2, 2], id4=[1] * 6,
323 v3=[1, 2, 3, 3, 3, 3]))
31def groupby(xs, keys):
32 result = defaultdict(list)
33 for (x, key) in zip(xs, keys):
34 result[key].append(x)
35 return result
29def _concat(df, type):
30 if df is None:
31 df = pd.DataFrame(_object_blocks[type])
32 else:
33 _df = pd.DataFrame(_object_blocks[type])
34 df = pd.concat([df, _df], sort=True)
35 return df
309@ApplyToDataframe
310def ungroup():
311 return UngroupDF
17def groupby_deco(func):
18 def func_wrapper(self, thing, *args, **kwargs):
19 if isinstance(thing, pd.core.groupby.DataFrameGroupBy):
20 agg = thing.apply(lambda x: func(self, x, *args, **kwargs))
21 is_series = isinstance(agg, pd.core.series.Series)
22 has_multiindex = isinstance(agg.index, pd.MultiIndex)
23 if is_series and has_multiindex:
24 return agg.unstack()
25 else:
26 return agg
27 return func(self, thing, *args, **kwargs)
28 return func_wrapper

Related snippets