EAE
EAE

Reputation: 13

Need to countplot the data types in a dataframe

I need to create a seaborn count plot for the different data types in a data frame. For instance in the example below, I should get a bar for float64 with a count of 3 and int64 with the count of 1.
I found someone did it by passing df.dtypes as an x or y parameter but I keep getting an error. TypeError: data type "" not understood Please does anyone know how to do this? Thanks for your help.

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

df = pd.DataFrame({"A":[12, 4, 5, None, 1],  
                   "B":[7, 2, 54, 3, None],  
                   "C":[20, 16, 11, 3, 8],  
                   "D":[14, 3, None, 2, 6]})  

print(df.dtypes)
sns.countplot(y=df.dtypes, data=df)
plt.show()

Upvotes: 1

Views: 1217

Answers (1)

l_l_l_l_l_l_l_l
l_l_l_l_l_l_l_l

Reputation: 538

You only need to pass the dtypes series, but make sure to convert the type objects to string. Matplotlib has trouble displaying labels that you passed as numpy.dtypes. Try this:

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

df = pd.DataFrame({"A":[12, 4, 5, None, 1],  
                   "B":[7, 2, 54, 3, None],  
                   "C":[20, 16, 11, 3, 8],  
                   "D":[14, 3, None, 2, 6]})  

sns.countplot(df.dtypes.map(str))
plt.show()

Output:

enter image description here

Upvotes: 1

Related Questions