Plot a graph for the data types of a DataFrame

Question:

I want to visualize the data types of my dataset as a part of exploratory data analysis and I have this data.

In [1]: df_planet = pd.DataFrame({'mass': [0.330, 4.87 , 5.97],
                                 'radius': [2439.7, 6051.8, 6378.1],
                                 'planet': ['Mercury', 'Venus', 'Earth']})
In [2]: df_planet.dtypes
Out[2]: mass      float64
        radius    float64
        planet     object
        dtype: object

How can I create a pie chart for the datatypes of df_planet (i.e. 66.67% float64 and 33.33% object)?

I tried the following code but it throws an error:

In [3]: plt.pie(df_planet.dtypes, autopct='%.0f%%')
Out[3]: TypeError: float() argument must be a string or a number, not 'numpy.dtype[float64]'
Asked By: Armando Bridena

||

Answers:

Are you looking for something like this?

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.pie.html

For example:

import pandas as pd
import matplotlib.pyplot as plt

df_planet.plot.pie(y='mass', labels=df_planet['planet'], figsize=(7, 7))
plt.legend(df_planet['planet'], title="Planet")
plt.show()
Answered By: Seasers

You need to count the data types.

import pandas as pd
import matplotlib.pyplot as plt

df_planet = pd.DataFrame({'mass': [0.330, 4.87 , 5.97],
                             'radius': [2439.7, 6051.8, 6378.1],
                             'planet': ['Mercury', 'Venus', 'Earth']})


plt.pie(df_planet.dtypes.value_counts(),autopct='%.0f%%')

plt.show()
Answered By: norie