数据分析三剑客【AIoT阶段一(下)】(十万字博文 保姆级讲解)—Matplotlib—数据可视化进阶(十七)

简介: 你好,感谢你能点进来本篇博客,请不要着急退出,相信我,如果你有一定的 Python 基础,想要学习 Python数据分析的三大库:numpy,pandas,matplotlib;这篇文章不会让你失望,本篇博客是 【AIoT阶段一(下)】 的内容:Python数据分析,

3.4 训练场

3.4.1 数据处理

  1. 加载数据,并查看相关信息:基金总数据条目,基金公司数量,基金总数量,基金总规模,查看前五条数据
  2. 将基金规模小于1亿元的数据过滤掉,将基金收益没有数据的过滤掉。
  3. 将基金规模和基金收益转换为浮点数,并将处理好的数据保存。


首先我们需要下载一个 Excel 文件:

链接: https://pan.baidu.com/s/1j2pn0vVN3-wJmSZ-01oiUg?pwd=niye

提取码: niye


下载完成之后,把该文件和我们的代码放到同一个文件夹下,这一操作我们在之前的博客中已经反复说到,这里就不再进行演示

数据查看:

import numpy as np
import pandas as pd
fund = pd.read_excel('./fund.xlsx')
print('基金总数据条目:', fund.shape)
print('基金公司一共有:', fund['公司'].nunique()) # 去重
print('基金总数量是:', fund['基金数量'].sum())
# 计算基金总规模
cnt = fund['基金规模'].str.endswith('亿元') # 判断是否以'亿元'结尾
fund2 = fund[cnt]     # 数据筛选
size = fund2['基金规模'].str[: -2].astype('float').sum() # 去掉'亿元'
print('基金总规模是:%0.2f亿元' % (size))
print('查看前五条数据:')
fund.head(5)

image.png

数据清洗:

import pandas as pd
fund = pd.read_excel('./fund.xlsx')
print('数据清洗前:', fund.shape)
# 过滤基金规模为空的数据
cnt = fund['基金规模'].str.endswith('亿元')
fund = fund[cnt]
# 过滤基金规模小于1亿的数据
cnt2 = fund['基金规模'].str[: -2].astype('float') > 1
fund = fund[cnt2]
# 过滤基金收益为空的数据
cnt3 = fund['基金收益'].str.endswith('%')
fund = fund[cnt3]
print('数据清洗后:', fund.shape)
fund.to_excel('./fund_clean.xlsx', index = False)
fund.head()

image.png

数据转换:

import pandas as pd
fund = pd.read_excel('./fund_clean.xlsx')
# 基金规模字符串转变为浮点数
fund['基金规模'] = fund['基金规模'].str[: -2].astype('float')
# 基金收益字符串转变为浮点数
def convert(x):
    x = x[: -1]
    x = float(x)
    return x
fund['基金收益'] = fund['基金收益'].apply(convert)
# 修改列名
fund.columns = ['姓名', '公司', '基金数量', '年', '天', '基金规模(亿元)', '基金收益(%)']
# 数据保存
fund.to_excel('./fund_end.xlsx', index = False)
fund.head(10)

1.png

3.4.2 数据挖掘与可视化

根据基金总规模,进行排序,水平条形图展示前十大公司

根据收益率,对所有数据进行降序排名,绘制前十佳基金经理。并将金额和收益率绘制到图片中。

十大基金公司:

%%time
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize = (12, 9))
sns.set_theme(style = 'darkgrid', context = 'talk', font = 'KaiTi')
fund = pd.read_excel('./fund_end.xlsx')
# 分组聚合
com = fund.groupby(by = '公司')[['基金规模(亿元)']].sum()
# 排序
com.sort_values(by = '基金规模(亿元)', 
                ascending = False,  # 降序排序
                inplace = True)     # 直接对原数据进行替换
# 行索引重置:变成自然数索引
com.reset_index(inplace = True)
# 画条形图
sns.barplot(x = '基金规模(亿元)', y = '公司',  # x轴和y轴
            data = com.iloc[: 10],    # 切片出来前十个
            orient = 'h')  # 水平条形图

2.png

收益十佳基金经理:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize = (12, 9))
sns.set_theme(style = 'darkgrid', context = 'talk', font = 'STKaiti')
fund = pd.read_excel('./fund_end.xlsx')
# 降序排序并直接替换原数据
fund.sort_values(by = '基金收益(%)', ascending = False, inplace = True)
sns.barplot(x = '基金收益(%)', y = '姓名',  
            data = fund.iloc[:10], orient = 'h',
            palette = 'Set1')  # 画板、颜色
for i in range(10):
    rate = fund.iloc[i]['基金收益(%)']
    pe = fund.iloc[i]['基金规模(亿元)']
    # 绘制基金规模
    plt.text(x = rate / 2, y = i, s = str(pe) + '亿元', ha = 'center', va = 'center')
    # 绘制基金收益
    plt.text(x = rate + 50, y = i, s = str(rate) + '%', va = 'center')
_ = plt.xlim(0, 2500) # 横坐标范围
_ = plt.xticks(np.arange(0, 2500, 200)) # 横坐标刻度    

3.png

目录
相关文章
|
2月前
|
数据可视化 数据挖掘 API
Python数据可视化利器Matplotlib详解
本文将深入探讨Python中常用的数据可视化库Matplotlib,介绍其基本概念、常见绘图函数和实例应用。通过学习Matplotlib,读者可以掌握如何利用Python进行数据可视化,展示数据分析结果。
|
2月前
|
数据可视化 数据挖掘 Python
Python数据可视化:探索Matplotlib的强大功能
数据可视化在如今的数据分析和展示中扮演着至关重要的角色。本文将介绍Python中常用的数据可视化库Matplotlib,深入探讨其功能和应用,帮助读者更好地利用Matplotlib进行数据可视化。
|
2月前
|
数据可视化 数据处理 Python
Python数据可视化库Matplotlib的应用与优势探究
本文将深入探讨Python中强大的数据可视化库Matplotlib的应用与优势。通过介绍Matplotlib的基本概念和常用功能,结合具体案例展示其在数据分析和图表绘制中的灵活性和实用性,帮助读者更好地利用这一工具进行数据可视化。
|
2月前
|
机器学习/深度学习 数据可视化 数据处理
Python数据可视化:探索Matplotlib库的强大功能
本文将深入探讨Python中用于数据可视化的重要工具之一——Matplotlib库。通过介绍Matplotlib库的基本概念、常用功能和实际应用案例,帮助读者更好地了解如何利用Matplotlib创建各种吸引人的数据图表。
|
2月前
|
数据可视化 搜索推荐 数据挖掘
Python数据可视化——探索Matplotlib库的强大功能
数据可视化在数据分析和展示中扮演着至关重要的角色,而Matplotlib作为Python中最流行的数据可视化库之一,具有丰富的功能和灵活性。本文将深入探讨Matplotlib库的基本用法和高级功能,带您领略数据可视化的魅力。
|
2月前
|
数据可视化 数据挖掘 Python
Python中的数据可视化利器Matplotlib详解
本文将深入探讨Python中一款强大的数据可视化工具——Matplotlib,介绍其基本用法、常见图表类型以及高级定制技巧,帮助读者更好地利用Matplotlib实现数据可视化需求。
|
12天前
|
数据可视化 数据挖掘 定位技术
Python 基于 Matplotlib 实现数据可视化(二)
Python 基于 Matplotlib 实现数据可视化(二)
22 0
|
13天前
|
数据可视化 数据挖掘 Python
Python中数据分析工具Matplotlib
【4月更文挑战第14天】Matplotlib是Python的数据可视化库,能生成多种图表,如折线图、柱状图等。以下是一个绘制简单折线图的代码示例: ```python import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [2, 4, 6, 8, 10] plt.figure() plt.plot(x, y) plt.title('简单折线图') plt.xlabel('X轴') plt.ylabel('Y轴') plt.show() ```
13 1
|
2月前
|
数据可视化 数据挖掘 Python
Python中的数据可视化工具Matplotlib简介与实践
在本文中,我们将介绍Python中常用的数据可视化工具Matplotlib,包括其基本概念、常用功能以及实际应用。通过学习Matplotlib,读者可以更好地理解和运用数据可视化技术,提升数据分析与展示的能力。
|
2月前
|
数据可视化 数据挖掘 API
Python数据分析中的数据可视化:Matplotlib与Seaborn的比较
在Python数据分析领域,数据可视化是至关重要的一环。本文将深入探讨两大流行的数据可视化库Matplotlib与Seaborn的异同,帮助读者更好地选择适合自身需求的工具。