Search code examples
pythonseabornfacet-grid

No Numeric Type To Aggregate


I have a dataframe in which there is the following columns:

Date - Seller - Amount

Code sample:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
testframe = pd.DataFrame({'date': ['01/02/2019', '01/02/2019', '01/02/2019', 
'02/02/2019','02/02/2019','03/02/2019'], 
'Seller': ['John', 'Ada', 'John', 'Ada', 'Ada', 'Ada'], 'Amount': 
[150,200,158,200,60,90]})

I aggregated the data so:

agg=pd.DataFrame(base.groupby(['date','Seller'])['Amount'].sum())
agg.reset_index(inplace=True)

Then I tried, using FacetGrid, to display the Amount along the days per Seller(each row from FacetGrid would be a row). Like so:

g=sns.FacetGrid(data=agg,row='Seller')
g.map(sns.lineplot, 'Amount', 'date')

But I get the following error:

No numeric types to aggregate

There was another post here on Stackoverflow, but didn't quite give a me clue about what to do in order to solve.

I checked the variables from packages and it returned numpy.int64. I tried to convert'em to float and int using .astype() but didn't work too.

Environment: Jupyter Notebook

Can someone shed a light on this?

Thanks in advance


Solution

  • Possibly you mean something like this:

    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    
    base = pd.DataFrame({'date': ['01/02/2019', '01/02/2019', '01/02/2019', 
                                       '02/02/2019','02/02/2019','03/02/2019'], 
        'Seller': ['John', 'Ada', 'John', 'Ada', 'Ada', 'Ada'], 'Amount': 
                [150,200,158,200,60,90]})
    
    
    base["date"] = pd.to_datetime(base["date"])
    
    agg=pd.DataFrame(base.groupby(['date','Seller'])['Amount'].sum())
    agg.reset_index(inplace=True)
    
    
    g=sns.FacetGrid(data=agg, row='Seller')
    g.map(sns.lineplot, 'date', 'Amount')
    
    plt.show()
    

    enter image description here

    Note that John's plot is empty because he only sold something on a single day, and one cannot draw a line from a point to itself.