IVI
IVI

Reputation: 2116

Pandas building a histogram

Price Postcode Type Town Date
2003-05-16 397500 BS22 7YA D WESTON-SUPER-MARE 2003-09-10 235000 PE9 3AE D STAMFORD 2003-11-28 150000 WA16 7RH F KNUTSFORD 2003-03-05 95000 PL13 1AT T LOOE 2003-09-19 112950 CF5 1HF T CARDIFF 2003-08-27 155000 CM0 7LY D SOUTHMINSTER 2003-11-17 147000 WD5 0BF F ABBOTS LANGLEY 2003-06-26 198000 CT6 6EE D HERNE BAY 2003-10-01 346000 LE2 2PD D LEICESTER 2003-10-13 163000 TD15 1QY T BERWICK-UPON-TWEED 2003-11-07 197000 W14 8HU F LONDON 2003-12-12 120000 SE7 7AB F LONDON 2003-03-10 110000 SY20 9LB D MACHYNLLETH 2003-08-22 46000 LL24 0RD T BETWS-Y-COED 2003-10-14 80000 CH7 6BQ T MOLD

I have a csv file like above and need to display a histogram of frequency of transactions according to price brackets. How can I achieve this using pandas?

Upvotes: 0

Views: 123

Answers (2)

BENY
BENY

Reputation: 323366

Using pd.cut, get the price brackets

df['G']=pd.cut(df.Price,10)

df1=df.G.value_counts()
df1
Out[123]: 
(116300.0, 151450.0]    3
(81150.0, 116300.0]     3
(186600.0, 221750.0]    2
(151450.0, 186600.0]    2
(45648.5, 81150.0]      2
(362350.0, 397500.0]    1
(327200.0, 362350.0]    1
(221750.0, 256900.0]    1
(292050.0, 327200.0]    0
(256900.0, 292050.0]    0
Name: G, dtype: int64
df1.plot(kind='bar')

enter image description here

Upvotes: 1

Marmaduke
Marmaduke

Reputation: 591

What do you mean by frequency of transactions? My first guess would be number of transactions per unit time, but then you have one value per price bracket, and a simple bar or line graph is more appropriate than a histogram.

If that's all you want, then (1) create a new row with your price brackets, (2) groupby() that row, then aggregate() with a custom function that calculates the frequency (3) Use Series.plot() to generate the final plot.

Upvotes: 0

Related Questions