Reputation: 101
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
Upvotes: 5
Views: 10772
Reputation: 570
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:
Libraries versions:
Upvotes: 3
Reputation: 1247
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:
df2.plot(kind='hist')
output of bin histogram is here:
Output of histogram of seconds:
df.sec.plot(kind='hist')
Upvotes: 2