Converting a Pandas Groupby Output from Series to Dataframe

Converting a Pandas GroupBy output from Series to DataFrame

g1 here is a DataFrame. It has a hierarchical index, though:

In [19]: type(g1)
Out[19]: pandas.core.frame.DataFrame

In [20]: g1.index
Out[20]:
MultiIndex([('Alice', 'Seattle'), ('Bob', 'Seattle'), ('Mallory', 'Portland'),
('Mallory', 'Seattle')], dtype=object)

Perhaps you want something like this?

In [21]: g1.add_suffix('_Count').reset_index()
Out[21]:
Name City City_Count Name_Count
0 Alice Seattle 1 1
1 Bob Seattle 2 2
2 Mallory Portland 2 2
3 Mallory Seattle 1 1

Or something like:

In [36]: DataFrame({'count' : df1.groupby( [ "Name", "City"] ).size()}).reset_index()
Out[36]:
Name City count
0 Alice Seattle 1
1 Bob Seattle 2
2 Mallory Portland 2
3 Mallory Seattle 1

Groupby a DataFrame into a new DataFrame with arange as index

You have a parameter as_index in groupby for that

df_a.groupby('id', as_index = False)['amount'].sum()

You get

    id  amount
0 1001 1710.34
1 1003 18.95
2 1004 321.20

Convert DataFrameGroupBy object to DataFrame pandas

The result of kl.aggregate(np.sum) is a normal DataFrame, you just have to assign it to a variable to further use it. With some random data:

>>> df = DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
>>> 'foo', 'bar', 'foo', 'foo'],
... 'B' : ['one', 'one', 'two', 'three',
... 'two', 'two', 'one', 'three'],
... 'C' : randn(8), 'D' : randn(8)})
>>> grouped = df.groupby('A')
>>> grouped
<pandas.core.groupby.DataFrameGroupBy object at 0x04E2F630>
>>> test = grouped.aggregate(np.sum)
>>> test
C D
A
bar -1.852376 2.204224
foo -3.398196 -0.045082

Using pandas groupby to write new information into the original DataFrame?

Check your output with reindex

df['new'] = grouped.agg(lambda x: idx(x)).reindex(pd.MultiIndex.from_frame(df)).values

Pandas groupby Increment datetime to make it unique for the group

You can use groupby().cumcount() with pd.to_timedelta:

df['ts'] += pd.to_timedelta(df.groupby('State').cumcount(), unit='s')

Output:

     State  a   b                         ts
0 Texas 4 6 2022-09-06 15:40:46.429416
1 Texas 5 10 2022-09-06 15:40:47.429416
2 Florida 1 3 2022-09-06 15:40:46.429416
3 Florida 3 11 2022-09-06 15:40:47.429416

python pandas - how to transform ds into dataframe

When grouping the DataFrame using more columns you get a MultiIndex.

You can use the reset_index method (see docs) to transform the MultiIndex into columns of a DataFrame.

For your example it would give something like:

> ds_perg1_2_merged.reset_index()

DescricaoProblema strRazaoSocial
0 Cobrança indevida. CAIXA ECONOMICA FEDERAL 66
1 Cobrança indevida. CAIXA SEGUROS S.A 45
2 Cobrança indevida. BANCO BMG S.A. 38
3 Cobrança indevida/abusiva CLARO S/A 50
4 Cobrança indevida/abusiva TIM CELULAR S/A 47
5 Cobrança indevida/abusiva COMPANHIA PIRATININGA DE FORÇA E LUZ 34
6 Produto com vício VIA VAREJO S/A 46
7 Produto com vício SAMSUNG ELETRONICA DA AMAZONIA LTDA 27
8 Produto com vício WHIRLPOOL S/A 23

How converting a pandas groupby/describe to standard dataframe?

You can specify column after groupby:

label = dataframe.groupby('label')['area'].describe()

In your solution remove first level of MultiIndex:

label = dataframe[['label', 'area']].groupby(['label']).describe().droplevel(0, axis=1)


Related Topics



Leave a reply



Submit