How to draw a histogram using pandas cut

Question:

This is my data:

df = pd.DataFrame(myList, index=None, columns=['seconds'])
df['count']= pd.cut(df['seconds'], bins = 30)

Categories (30, interval[float64]): [(0.0871, 0.117] < (0.117, 0.145] < (0.145, 0.174] <
                                     (0.174, 0.202] ... (0.83, 0.858] < (0.858, 0.887] <
                                     (0.887, 0.915] < (0.915, 0.944]]

How do I draw a histogram from this result? (x axis is the floating values, and the y is the total counts of values in each bucket?) I saw a lot of posts using "kind=bar" to draw but I want to know if it is possible to draw this data by using histogram ?

Thanks everyone

Asked By: pkim

||

Answers:

Here is a simple example… hope it helps:

import random
df = pd.DataFrame([random.randint(1,1000) for i in range(0,100)], columns=['sec'])
df['bin']=pd.cut(df['sec'], bins = 30).astype(str)
df2 = df.groupby('bin').bin.count()
# Fixed to show distribution of bin
df2.plot(kind='bar')

output:

enter image description here

df2.plot(kind='hist')

output of bin histogram is here:

enter image description here

Output of histogram of seconds:

df.sec.plot(kind='hist')

enter image description here

Answered By: frankr6591

An answer using library seaborn‘s function countplot , after using pandas.cut function:

import numpy as np
import pandas as pd
import seaborn as sns

bins = list(range(0, 110, 10))
bins.append(float('inf'))
np.random.seed(31415)
data_age = np.random.randint(low=0, high=120, size=200)

df_test = pd.DataFrame({'age': data_age, 
                        'age_group': pd.cut(data_age, bins=bins, right=False)})

ax = sns.countplot(data=df_test, x='age_group')
ax.tick_params(axis='x', labelrotation=90)

This builds this kind of image:

countplot_with_categories

Libraries versions:

  • Numpy: 1.18.0
  • Pandas: 1.1.5
  • Seaborn: 0.11.0
Answered By: Augusto Sisa
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.