-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting.py
More file actions
32 lines (16 loc) · 749 Bytes
/
Copy pathplotting.py
File metadata and controls
32 lines (16 loc) · 749 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import pandas as pd
import matplotlib.pyplot as plt
def create_graph(all_books):
df_books = pd.DataFrame(all_books)
rating_map = {'One': 1, 'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5}
df_books['rating/5'] = df_books['rating/5'].map(rating_map)
df_books['price'] = df_books['price'].str.replace('£', '').str.strip().astype(float)
df_books.to_csv('Data/books.csv', index=False)
avg_price_by_rating = df_books.groupby('rating/5')['price'].mean()
plt.figure(figsize=(6,4))
plt.bar(avg_price_by_rating.index, avg_price_by_rating.values, color='orange')
plt.xlabel("Rating/5")
plt.ylabel("Average Price (£)")
plt.title("Average Book Price by Rating")
plt.xticks(range(1,6))
plt.show()