分析商品价格与利润的关系,评估价格敏感度。
| product_id | category | price | sales | profit |
|---|---|---|---|---|
| 101 | Electronics | 1000 | 100 | 20000 |
| 102 | Electronics | 1500 | 80 | 24000 |
| 103 | Clothing | 200 | 500 | 30000 |
| 104 | Clothing | 300 | 300 | 27000 |
| 105 | Home | 500 | 200 | 30000 |
| 106 | Home | 800 | 150 | 30000 |
| 107 | Electronics | 2000 | 50 | 25000 |
| 108 | Clothing | 150 | 600 | 24000 |
| 109 | Home | 400 | 250 | 30000 |
| 110 | Electronics | 1200 | 90 | 27000 |
import pandas as pd
import numpy as np
# 读取数据
data = '''product_id,category,price,sales,profit
101,Electronics,1000,100,20000
102,Electronics,1500,80,24000
103,Clothing,200,500,30000
104,Clothing,300,300,27000
105,Home,500,200,30000
106,Home,800,150,30000
107,Electronics,2000,50,25000
108,Clothing,150,600,24000
109,Home,400,250,30000
110,Electronics,1200,90,27000
'''
# 转换为DataFrame
from io import StringIO
df = pd.read_csv(StringIO(data))
print("原始数据:")
print(df)
print("\n数据基本信息:")
print(df.info())
print("\n数据描述性统计:")
print(df.describe())
# 计算单价利润
df['profit_per_unit'] = df['profit'] / df['sales']
print("\n添加单价利润后的数据:")
print(df)
# 计算利润率
df['profit_margin'] = (df['profit'] / (df['price'] * df['sales'])) * 100
print("\n添加利润率后的数据:")
print(df)
# 分析价格与销量的关系
price_sales_corr = df['price'].corr(df['sales'])
print(f"\n价格与销量的相关系数: {price_sales_corr:.4f}")
# 分析价格与利润的关系
price_profit_corr = df['price'].corr(df['profit'])
print(f"价格与利润的相关系数: {price_profit_corr:.4f}")
# 分析销量与利润的关系
sales_profit_corr = df['sales'].corr(df['profit'])
print(f"销量与利润的相关系数: {sales_profit_corr:.4f}")
# 按类别分析
print("\n按类别分析:")
category_analysis = df.groupby('category').agg({
'price': 'mean',
'sales': 'mean',
'profit': 'mean',
'profit_per_unit': 'mean',
'profit_margin': 'mean'
})
print(category_analysis)
# 分析价格敏感度(价格弹性)
# 计算价格弹性:销量变化百分比 / 价格变化百分比
def calculate_price_elasticity(df):
elasticity = []
for i in range(1, len(df)):
price_change = (df['price'].iloc[i] - df['price'].iloc[i-1]) / df['price'].iloc[i-1]
sales_change = (df['sales'].iloc[i] - df['sales'].iloc[i-1]) / df['sales'].iloc[i-1]
if price_change != 0:
elasticity.append(sales_change / price_change)
return np.mean(elasticity) if elasticity else 0
# 按类别计算价格弹性
print("\n按类别计算价格弹性:")
for category in df['category'].unique():
category_df = df[df['category'] == category].sort_values('price')
elasticity = calculate_price_elasticity(category_df)
print(f"{category}: {elasticity:.4f}")
# 找出利润最高的商品
print("\n利润最高的商品:")
print(df.sort_values('profit', ascending=False).head(5))
# 找出利润率最高的商品
print("\n利润率最高的商品:")
print(df.sort_values('profit_margin', ascending=False).head(5))
# 分析价格区间与利润的关系
df['price_range'] = pd.cut(df['price'], bins=[0, 500, 1000, 2000], labels=['低价格', '中价格', '高价格'])
print("\n按价格区间分析:")
price_range_analysis = df.groupby('price_range').agg({
'sales': 'mean',
'profit': 'mean',
'profit_margin': 'mean'
})
print(price_range_analysis)