Solving Pandas Error – ‘No numeric types to aggregate’ error when Pivoting a Pandas Dataframe containing strings

‘No numeric types to aggregate’ error when Pivoting a Pandas Dataframe containing strings is an error which occurs when you want to pivot a data frame with string values and you make a mistake while doing it.

In this article I am going to explain why the error is happening and how to install Selenium properly without getting the error again. Also we are going to check out other solutions that may work for your particular case.

Exploring the Error : ‘No numeric types to aggregate’ error when Pivoting a Pandas Dataframe containing strings

This error occurs when you want to pivot a data frame with string values and you make a mistake while doing it.

The error message should look like the message bellow. Make sure you are not dealing with another error.

                                                                       #
DataError: No numeric types to aggregate
                                                                       #

In the sections bellow we will explain the root of the error more and propose a set of possible fixes and options

Solution : using .aggregate() only, pivot_table or a lambda function

The error happens because np.sum does not know how to deal with strings.

One important thing you need to keep in mind is that depending on the pandas version you have the syntax of pivot_table will be different.

Bellow is a set of options and methods you can use according to your needs

Fix using .aggregate() only

                                                                       #
df1 = df.groupby(["id","contact_id","Network_Name","question"])['response_answer'].aggregate('first').unstack().reset_index()
df1.columns=df1.columns.tolist()
print (df1)
                                                                       #

Fix using pivot_table

                                                                       #
pivot_table = unified_df.pivot_table(index=['id', 'contact_id'], columns='question', values='response_answer', aggfunc=lambda x: ' '.join(x))
                                                                       #

Fix using a lambda function

                                                                       #
df1 = df.groupby(["id","contact_id","Network_Name","question"])['response_answer'].aggregate(lambda x: x).unstack().reset_index()
df1.columns=df1.columns.tolist()
print (df1)
                                                                       #

I hope the fixes above fixed your problem, good luck with the scripts to come.

Summing-up

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

I hope my article was helpful in solving this error, errors are part of our journey of coding with Python, keep learning, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/