How to delete aggregated columns in a Dataframe Pandas obtained through the 'pivot_table'?

Asked

Viewed 89 times

-1

Hello, I managed a Dataframe in Pandas with the following command:

df_projects7 = pd.pivot_table(df_projects8, index=['id_prj', 'Nome do Projeto', 'criado_em'], values=['Percent_executado', 'Atualizado'], aggfunc=[np.min, np.mean])

With the following result: The columns 'Id_prj', 'Project Name' and 'created in', remained as indexes and were aggregated columns: Amin with 'Percent_executed', Amin with 'Updated' and Mean with 'Percent_executed', Mean with 'Updated'.

The df_projects7.dtypes command returns:

Amin Updated int16 Percent_executado int64 Mean Updated float64 Percent_executado float64 dtype: Object

I need to delete the columns: Amin with 'Percent_run' and Mean with 'Updated'. Because only the columns make sense in context: Amin with 'Updated' and Mean with 'Percent_executed'

Thanks in advance.

  • Hello, Jcrrocha. Can you share with us a sample of the data so that it is reproducible? can be very small with up to 5 or 10 lines. Read here how to create an example Minimum, Complete and Verifiable

  • Thanks for the help, however I ended up skirting the problem and found a solution that served me. I made a pivot_table for Percent_performed with 'Amin' and another pivot_table for 'Updated' with the 'Mean' function and then merged the two generated dataframes. Thanks!

1 answer

0

I believe you are behind the dataframe.drop function().

df_projects7=df_projects7.drop(['Percent_executado','Atualizado'], axis=1)

Let me know if it works.

  • Hello, thank you for your reply, however it did not work (Keyerror: 'Percent_run'). Note that it is not a common dataframe, at least I think, as it was obtained with the pivot_table that transforms it as if it were a dynamic excel table. It creates hierarchical levels : 'Amin' and 'Mean' and under each of them 'Percent_executed' and 'Updated', so I have two columns with 'Percent_executed' and two with 'Updated'. And I want to delete the 'Percent_executed' column that is below 'Amin' and the 'Updated' column that is below 'Mean', the other two need to keep. Blz.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.