Katie
Katie

Reputation: 1031

Seaborn's No numeric types to aggregate error

I have a pandas data frame in this form:

    Country     Year        Value
0   Thailand    1989       48587.03
1   Thailand    1990       55903.07
2   Vietnam     1989      100290.04
3   Vietnam     1990      118873.59
4   India       1989      147383.02
5   India       1990      178230.05

The dtype of the values in Value is float.

I am using seaborn to float this:

sns.lineplot(x='Year', y='Value', hue='Country', data=topfiveot)

And got an error:

DataError: No numeric types to aggregate

What possibly has caused the issue?

Upvotes: 12

Views: 9732

Answers (3)

Nikhil Gupta
Nikhil Gupta

Reputation: 1486

I had a similar issue and it turned out the one of the columns that I was plotting was of type object although it had all "numeric values". Type casting it to float worked for me

Upvotes: 0

Augustin C Hennings
Augustin C Hennings

Reputation: 71

I think this error has more to do with pandas.groupby() than with seaborn itself. Checking to make sure that all of my numeric columns were floats worked for me. In your case

df.Year = df.Year.astype(float) and df.Values = df.Values.astype(float)

Upvotes: 7

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339220

I suppose you want to show a plot like

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"Country" : ["Thailand"]*2 + ["Vietnam"]*2 + ["India"]*2,
                   "Year" : np.tile([1989,1990], 3),
                   "Value" : [8,3,10,4,3,0]})

ax = df.pivot("Year", "Country", "Value").plot()
ax.ticklabel_format(axis='x', useOffset=False)
plt.show()

enter image description here

The exact same plot can be achieved with seaborn via

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.DataFrame({"Country" : ["Thailand"]*2 + ["Vietnam"]*2 + ["India"]*2,
                   "Year" : np.tile([1989,1990], 3),
                   "Value" : [8,3,10,4,3,0]})

ax = sns.lineplot(x='Year', y='Value', hue='Country', data=df)
ax.ticklabel_format(axis='x', useOffset=False)
plt.show()

Upvotes: -1

Related Questions