Python 数据分析实战:宠物经济行业发展洞察

目录

一、案例背景

二、代码实现

2.1 数据收集

2.2 数据探索性分析

2.3 数据清洗

2.4 数据分析

2.4.1 宠物用品用户满意度分析

2.4.2 宠物用品销售与价格关系分析

2.4.3 宠物经济行业未来发展预测

三、主要的代码难点解析

3.1 数据收集

3.2 数据清洗 - 销售数据处理

3.3 数据分析 - 宠物用品用户满意度分析

3.4 数据分析 - 宠物用品销售与价格关系分析

3.5 数据可视化

四、可能改进的代码

4.1 数据收集改进

4.2 数据清洗改进

4.3 数据分析改进


一、案例背景

近年来,随着人们生活水平的提高和情感需求的增长,宠物经济呈现出蓬勃发展的态势。从宠物食品、用品到宠物医疗、美容、培训等服务,宠物经济涵盖了多个领域。然而,行业的快速发展也带来了市场竞争加剧、服务质量参差不齐等问题。通过 Python 对宠物经济行业相关数据进行深入分析,有助于了解行业现状、把握市场趋势、发现潜在商机,为宠物企业制定战略、优化产品和服务提供数据支持。

二、代码实现

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import requests
from bs4 import BeautifulSoup

2.1 数据收集

数据来源包括行业研究报告网站(如艾媒咨询、华经产业研究院)、电商平台销售数据、社交媒体上的宠物相关话题讨论以及宠物协会发布的统计信息。

  • 从艾媒咨询网站抓取宠物经济市场规模数据:

url = 'https://www.iimedia.cn/research/pet_economy.html'
headers = {
    'User - Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers = headers)
soup = BeautifulSoup(response.text, 'html.parser')
market_size_data = []
div = soup.find('div', class_='market - size - container')
items = div.find_all('li')
for item in items:
    year = item.find('span', class_='year').text.strip()
    market_size = float(item.find('span', class_='size').text.strip().replace('亿元', ''))
    market_size_data.append({'Year': year, 'Market_Size': market_size})
market_size_df = pd.DataFrame(market_size_data)

  • 从电商平台 API 获取宠物用品销售数据(假设已有 API 接口,实际需申请权限):

import json
api_url = 'https://api.ecommerceplatform.com/pet_products_sales'
headers = {
    'Authorization': 'your_api_key',
    'Content - Type': 'application/json'
}
response = requests.get(api_url, headers = headers)
if response.status_code == 200:
    sales_data = json.loads(response.text)
    sales_df = pd.DataFrame(sales_data)
else:
    print('Failed to get sales data')

2.2 数据探索性分析

# 查看市场规模数据基本信息
print(market_size_df.info())
# 查看销售数据基本信息
print(sales_df.info())

# 分析宠物经济市场规模随时间变化趋势
market_size_df['Year'] = pd.to_numeric(market_size_df['Year'])
plt.figure(figsize=(12, 6))
sns.lineplot(x='Year', y='Market_Size', data=market_size_df)
plt.title('Trend of Pet Economy Market Size')
plt.xlabel('Year')
plt.ylabel('Market Size (billion yuan)')
plt.show()

# 查看不同类型宠物用品销售数量分布
product_count = sales_df['Product_Type'].value_counts()
plt.figure(figsize=(10, 6))
sns.barplot(x=product_count.index, y=product_count.values)
plt.title('Distribution of Pet Product Sales by Type')
plt.xlabel('Product Type')
plt.ylabel('Sales Count')
plt.xticks(rotation=45)
plt.show()

2.3 数据清洗

# 市场规模数据清洗
# 检查并处理缺失值
market_size_df.dropna(inplace = True)
# 去除重复记录
market_size_df = market_size_df.drop_duplicates()

# 销售数据清洗
# 处理异常销售数据
sales_df = sales_df[(sales_df['Sales_Volume'] > 0) & (sales_df['Price'] > 0)]

2.4 数据分析

你可能感兴趣的:(python,python,数据分析,宠物)