Untitled
import pandas as pd import matplotlib.pyplot as plt import random genres = ['Sci-Fi', 'Fantasy', 'Action', 'Mystery', 'Horror'] ratings = [1, 2, 3, 4, 5] data = {'Genre': [], 'Rating': [], 'Price': []} df = pd.DataFrame(data) num_books = 20 for _ in range(num_books): genre_choices = ['Sci-Fi', 'Fantasy', 'Action', 'Mystery', 'Horror'] genre_weights = [0.4, 0.2, 0.2, 0.1, 0.1] chosen_genre = random.choices(genre_choices, weights=genre_weights)[0] chosen_rating = random.choice(ratings) chosen_price = random.randint(10, 50) new_book = {'Genre': chosen_genre, 'Rating': chosen_rating, 'Price': chosen_price} df = pd.concat([df, pd.DataFrame([new_book])], ignore_index=True) df.to_csv('books_new.csv', index=False) print("Data saved to books_new.csv") genre_counts = df['Genre'].value_counts() plt.figure(figsize=(10, 4)) plt.bar(genre_counts.index, genre_counts.values) plt.title('Number of Books per Genre') plt.xlabel('Genre') plt.ylabel('Number of Books') plt.show() rating_counts = df['Rating'].value_counts() plt.figure(figsize=(8, 8)) plt.pie(rating_counts, labels=rating_counts.index, autopct='%1.1f%%') plt.title('Distribution of Ratings') plt.show() plt.figure(figsize=(10, 4)) plt.scatter(df['Price'], df['Rating']) plt.title('Price vs. Rating') plt.xlabel('Price') plt.ylabel('Rating') plt.show() print("Charts displayed!")
Leave a Comment