python 数据分析第一天¶
如下列表保存着本公司从2022年1月到12月五个销售区域(南京、无锡、苏州、徐州、南通)的销售额(以百万元为单位),请利用这些数据完成以下操作:
python
sales_month = [f'{i:>2d}月' for i in range(1, 13)]
sales_area = ['南京', '无锡', '苏州', '徐州', '南通']
sales_data = [
[32, 17, 12, 20, 28],
[41, 30, 17, 15, 35],
[35, 18, 13, 11, 24],
[12, 42, 44, 21, 34],
[29, 11, 42, 32, 50],
[10, 15, 11, 12, 26],
[16, 28, 48, 22, 28],
[31, 40, 45, 30, 39],
[25, 41, 47, 42, 47],
[47, 21, 13, 49, 48],
[41, 36, 17, 36, 22],
[22, 25, 15, 20, 37]
]
- 统计本公司每个月的销售额。
- 统计本公司销售额的月环比。
- 统计每个销售区域全年的销售额。
- 按销售额从高到低排序销售区域及其销售额。
- 统计全年最高的销售额出现在哪个月哪个区域。
- 找出哪个销售区域的业绩最不稳定。
In [12]:
sales_month = [f'{i:>2d}月' for i in range(1, 13)]
sales_area = ['南京', '无锡', '苏州', '徐州', '南通']
sales_data = [
[32, 17, 12, 20, 28],
[41, 30, 17, 15, 35],
[35, 18, 13, 11, 24],
[12, 42, 44, 21, 34],
[29, 11, 42, 32, 50],
[10, 15, 11, 12, 26],
[16, 28, 48, 22, 28],
[31, 40, 45, 30, 39],
[25, 41, 47, 42, 47],
[47, 21, 13, 49, 48],
[41, 36, 17, 36, 22],
[22, 25, 15, 20, 37]
]
#1. 统计本公司每个月的销售额。
month_sales = []
for i,month in enumerate(sales_month):
temp = sum(sales_data[i])
month_sales.append()
print(f'{month}:{temp}')
1月:109 2月:138 3月:101 4月:153 5月:164 6月:74 7月:142 8月:185 9月:202 10月:178 11月:152 12月:119
In [25]:
#2. 统计本公司销售额的月环比。
for i,month in enumerate(sales_month):
if i > 0:
huan = (sum(sales_data[i]) - sum(sales_data[i-1]))/sum(sales_data[i-1])
print(f"{month}:{huan:.2%}")
1月:-21.71% 2月:26.61% 3月:-26.81% 4月:51.49% 5月:7.19% 6月:-54.88% 7月:91.89% 8月:30.28% 9月:9.19% 10月:-11.88% 11月:-14.61% 12月:-21.71%
In [32]:
#3. 统计每个销售区域全年的销售额。
from statistics import pvariance as var
my_dict={}
area_vars = []
for j,area in enumerate(sales_area):
temp = [sales_data[i][j] for i in range(12)]
area_vars.append(var(temp))
my_dict[area]=sum(temp)
print(f'{area}:{sum(temp)}')
南京:341 无锡:324 苏州:324 徐州:310 南通:418
In [39]:
# 4. 按销售额从高到低排序销售区域及其销售额。
list1 = sorted(my_dict,key=lambda x:my_dict[x], reverse=True)
for key in list1:
print(f'{key}:{my_dict[key]}')
南通:418 南京:341 无锡:324 苏州:324 徐州:310
In [38]:
# 5. 统计全年最高的销售额出现在哪个月哪个区域。
max1 = 0
yue = 0
di = 0
for x,y in enumerate(sales_data):
for z,t in enumerate(y):
if t >= max1:
max1 = t
yue = x
di = z
print(f'''
最高销售额:{max1}
月份:{sales_month[yue]}
地区:{sales_area[di]}
''')
最高销售额:50 月份: 5月 地区:南通
In [30]:
# 6. 找出哪个销售区域的业绩最不稳定。
sales_area[area_vars.index(max(area_vars))]
Out[30]:
'苏州'
三大神器¶
- Numpy - numerical python - ndarray
- pandas - panel date set - series / dateframe / index - 数据分析全流程工具 / cuDF Polars
- matplotlib - 封装了常用的统计图表
In [41]:
# %pip install numpy pandas matplotlib openpyxl
In [42]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [43]:
np.__version__
Out[43]:
'2.2.5'
In [44]:
pd.__version__
Out[44]:
'2.2.3'
In [45]:
array = np.array(sales_data)
array
Out[45]:
array([[32, 17, 12, 20, 28], [41, 30, 17, 15, 35], [35, 18, 13, 11, 24], [12, 42, 44, 21, 34], [29, 11, 42, 32, 50], [10, 15, 11, 12, 26], [16, 28, 48, 22, 28], [31, 40, 45, 30, 39], [25, 41, 47, 42, 47], [47, 21, 13, 49, 48], [41, 36, 17, 36, 22], [22, 25, 15, 20, 37]])
In [46]:
array.sum()
Out[46]:
np.int64(1717)
In [47]:
array.sum(axis=1)
Out[47]:
array([109, 138, 101, 153, 164, 74, 142, 185, 202, 178, 152, 119])
In [52]:
array.sum(axis=0)
Out[52]:
array([341, 324, 324, 310, 418])
In [ ]:
array.sum(axis=0).argmax()
In [ ]:
array.var(axis=0).argmax()
In [55]:
df = pd.DataFrame(
data=array,
columns=sales_area,
index =sales_month
)
df
Out[55]:
南京 | 无锡 | 苏州 | 徐州 | 南通 | |
---|---|---|---|---|---|
1月 | 32 | 17 | 12 | 20 | 28 |
2月 | 41 | 30 | 17 | 15 | 35 |
3月 | 35 | 18 | 13 | 11 | 24 |
4月 | 12 | 42 | 44 | 21 | 34 |
5月 | 29 | 11 | 42 | 32 | 50 |
6月 | 10 | 15 | 11 | 12 | 26 |
7月 | 16 | 28 | 48 | 22 | 28 |
8月 | 31 | 40 | 45 | 30 | 39 |
9月 | 25 | 41 | 47 | 42 | 47 |
10月 | 47 | 21 | 13 | 49 | 48 |
11月 | 41 | 36 | 17 | 36 | 22 |
12月 | 22 | 25 | 15 | 20 | 37 |
In [56]:
df.sum(axis=1)
Out[56]:
1月 109 2月 138 3月 101 4月 153 5月 164 6月 74 7月 142 8月 185 9月 202 10月 178 11月 152 12月 119 dtype: int64
In [57]:
df.sum(axis=0)
Out[57]:
南京 341 无锡 324 苏州 324 徐州 310 南通 418 dtype: int64
In [58]:
df["总计"] = df.sum(axis=1)
df
Out[58]:
南京 | 无锡 | 苏州 | 徐州 | 南通 | 总计 | |
---|---|---|---|---|---|---|
1月 | 32 | 17 | 12 | 20 | 28 | 109 |
2月 | 41 | 30 | 17 | 15 | 35 | 138 |
3月 | 35 | 18 | 13 | 11 | 24 | 101 |
4月 | 12 | 42 | 44 | 21 | 34 | 153 |
5月 | 29 | 11 | 42 | 32 | 50 | 164 |
6月 | 10 | 15 | 11 | 12 | 26 | 74 |
7月 | 16 | 28 | 48 | 22 | 28 | 142 |
8月 | 31 | 40 | 45 | 30 | 39 | 185 |
9月 | 25 | 41 | 47 | 42 | 47 | 202 |
10月 | 47 | 21 | 13 | 49 | 48 | 178 |
11月 | 41 | 36 | 17 | 36 | 22 | 152 |
12月 | 22 | 25 | 15 | 20 | 37 | 119 |
In [61]:
df["月环比"]=df["总计"].pct_change()
df
Out[61]:
南京 | 无锡 | 苏州 | 徐州 | 南通 | 总计 | 月环比 | |
---|---|---|---|---|---|---|---|
1月 | 32 | 17 | 12 | 20 | 28 | 109 | NaN |
2月 | 41 | 30 | 17 | 15 | 35 | 138 | 0.266055 |
3月 | 35 | 18 | 13 | 11 | 24 | 101 | -0.268116 |
4月 | 12 | 42 | 44 | 21 | 34 | 153 | 0.514851 |
5月 | 29 | 11 | 42 | 32 | 50 | 164 | 0.071895 |
6月 | 10 | 15 | 11 | 12 | 26 | 74 | -0.548780 |
7月 | 16 | 28 | 48 | 22 | 28 | 142 | 0.918919 |
8月 | 31 | 40 | 45 | 30 | 39 | 185 | 0.302817 |
9月 | 25 | 41 | 47 | 42 | 47 | 202 | 0.091892 |
10月 | 47 | 21 | 13 | 49 | 48 | 178 | -0.118812 |
11月 | 41 | 36 | 17 | 36 | 22 | 152 | -0.146067 |
12月 | 22 | 25 | 15 | 20 | 37 | 119 | -0.217105 |
In [73]:
plt.get_cmap("YlOrRd")
Out[73]:
YlOrRd
under
bad
over
In [74]:
df.style.format(
formatter={
"月环比":"{:.2%}"
},
na_rep="-----"
).background_gradient(
cmap="YlOrRd",
subset=["总计","月环比"]
)
Out[74]:
南京 | 无锡 | 苏州 | 徐州 | 南通 | 总计 | 月环比 | |
---|---|---|---|---|---|---|---|
1月 | 32 | 17 | 12 | 20 | 28 | 109 | ----- |
2月 | 41 | 30 | 17 | 15 | 35 | 138 | 26.61% |
3月 | 35 | 18 | 13 | 11 | 24 | 101 | -26.81% |
4月 | 12 | 42 | 44 | 21 | 34 | 153 | 51.49% |
5月 | 29 | 11 | 42 | 32 | 50 | 164 | 7.19% |
6月 | 10 | 15 | 11 | 12 | 26 | 74 | -54.88% |
7月 | 16 | 28 | 48 | 22 | 28 | 142 | 91.89% |
8月 | 31 | 40 | 45 | 30 | 39 | 185 | 30.28% |
9月 | 25 | 41 | 47 | 42 | 47 | 202 | 9.19% |
10月 | 47 | 21 | 13 | 49 | 48 | 178 | -11.88% |
11月 | 41 | 36 | 17 | 36 | 22 | 152 | -14.61% |
12月 | 22 | 25 | 15 | 20 | 37 | 119 | -21.71% |
In [78]:
plt.rcParams['font.sans-serif'].insert(0, 'SimHei')
plt.rcParams['axes.unicode_minus'] = False
In [79]:
#魔法指令
%config InlineBackend.figure_format = 'svg'
In [86]:
df.plot(
figsize=(8,5),
kind="line",
y="总计",
legend=False,
marker="*",
linewidth=0.6,
linestyle="--",
)
plt.ylim(0,240)
plt.title("2022年月度销售额",fontdict={"size":18,"color":"navy"})
plt.show()
In [85]:
df.plot(
figsize=(12,5),
kind="bar",
y=sales_area,
stacked=True,
)
plt.xticks(rotation=0)
plt.show()
In [87]:
df.to_excel("销售数据.xlsx")