Keithx
Keithx

Reputation: 3148

Misleading Index During Series to DataFrame Pandas Transformation

I have created the aggregated function in pandas and save the result:

import pandas as pd
_dwh = df2_date[df2_date.STATUS == 'A']
       .groupby('Party_id')
       .DURATION_DWH.agg(np.mean)

Result looks like this:

enter image description here

Then, I tried to switch into a pandas DataFrame as follows:

df2_dwh = pd.DataFrame(_dwh)

It returned some confusing results:

enter image description here

How can I create a normal DataFrame with indices like 1,...,n and Party_id and Duration_DWH like columns.

Thanks

Upvotes: 0

Views: 24

Answers (1)

jezrael
jezrael

Reputation: 862671

You need add parameter as_index=False or reset_index:

_dwh=df2_date[df2_date.STATUS=='A'].groupby('Party_id', as_index=False).DURATION_DWH.mean()

print (_dwh)
                 Party_id  DURATION_DWH
0  214BB440D604466275DFBB         574.0
1  214BB440D604466276D1B3         574.0
2  214BB440D604466371D1B2         558.5
3  214BB440D604466371DDB1         578.0
4  214BB440D604466373DBB5         578.0

_dwh=df2_date[df2_date.STATUS=='A'].groupby('Party_id', as_index=False).DURATION_DWH
                                   .mean()
                                   .reset_index()

print (_dwh)

                     Party_id  DURATION_DWH
    0  214BB440D604466275DFBB         574.0
    1  214BB440D604466276D1B3         574.0
    2  214BB440D604466371D1B2         558.5
    3  214BB440D604466371DDB1         578.0
    4  214BB440D604466373DBB5         578.0

Upvotes: 1

Related Questions