0304_数据可视化实战(一)

数据处理

安装openpyxl

# 当前执行的命令是安装在该虚拟python环境中
!pip install openpyxl -i https://mirrors.aliyun.com/pypi/simple/

数据查看

import pandas as pd
fund = pd.read_excel('./fund.xlsx')
# 查看前10条数据
fund.head(10)
姓名公司基金数量基金规模基金收益
0艾定飞华商基金221357.23亿元60.88%
1艾小军国泰基金19788793.09亿元133.26%
2安昀长信基金4635348.11亿元176.47%
3彬彬人保资产62855.23亿元51.72%
4包兵华鹏华基金41355148.30亿元102.21%
5白冰洋中银证券441741.84亿元20.95%
6薄官辉银华基金5534734.73亿元160.91%
7边慧银华基金201444.33亿元0.15%
8白海峰招商基金10433230.00亿元93.60%
9白洁中银基金1310261161.36亿元44.71%
# 基金总数据条目
fund.shape # (2550, 7)

# 去重:基金公司数量
fund['公司'].nunique() # 159

# 基金总数量
fund['基金数量'].sum() # 16045

# 计算基金总规模
cond = fund['基金规模'].str.endswith('亿元')  # 判断是否以"亿元"结尾
fund2 = fund[cond] # 数据筛选

# 计算基金总规模
size = fund2['基金规模'].str[:-2].astype('float').sum()
size # 298453.42000000004

数据清洗转换

# 过滤基金规模为亿元结尾的数据
cond = fund['基金规模'].str.endswith('亿元')
fund = fund[cond]

# 过滤基金规模小于1亿的数据
cond2 = fund['基金规模'].str[:-2].astype('float') > 1
fund = fund[cond2]

# 过滤基金收益为空的数据
cond3 = fund['基金收益'].str.endswith('%')
fund = fund[cond3]

# 清洗后的数据保存到新的excel
fund.to_excel('./fund_clean.xlsx', index = False)
fund.head()
姓名公司基金数量基金规模基金收益
0艾定飞华商基金221357.23亿元60.88%
1艾小军国泰基金19788793.09亿元133.26%
2安昀长信基金4635348.11亿元176.47%
3彬彬人保资产62855.23亿元51.72%
4包兵华鹏华基金41355148.30亿元102.21%
fund = pd.read_excel('./fund_clean.xlsx')

# 基金规模字符串转变为浮点数
fund['基金规模'] = fund['基金规模'].str[:-2].astype('float')

# 基金收益字符串转变为浮点数
fund['基金收益'] = fund['基金收益'].str[:-1].astype('float')


# 修改列名
fund.columns = ['姓名', '公司', '基金数量', '年', '天','基金规模(亿元)', '基金收益(%)']

# 数据保存
fund.to_excel('./fund_result.xlsx',index = False)
fund.head(10)
姓名公司基金数量基金规模(亿元)基金收益(%)
0艾定飞华商基金221357.2360.88
1艾小军国泰基金19788793.09133.26
2安昀长信基金4635348.11176.47
3彬彬人保资产62855.2351.72
4包兵华鹏华基金41355148.30102.21
5白冰洋中银证券441741.8420.95
6薄官辉银华基金5534734.73160.91
7边慧银华基金201444.330.15
8白海峰招商基金10433230.0093.60
9白洁中银基金1310261161.3644.71

数据可视化

基金规模前10公司

%%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='SimHei')

fund = pd.read_excel('./fund_result.xlsx')

# 分组聚合
com = fund.groupby(by = '公司')[['基金规模(亿元)']].sum()

# 排序,ascending = False降序;inplace=True:在原始 DataFrame 上进行排序
com.sort_values(by = '基金规模(亿元)',ascending = False, inplace = True)
# 行索引重置
com.reset_index(inplace = True)
com.head()
Wall time: 305 ms
公司基金规模(亿元)
0易方达基金19795.79
1天弘基金16402.23
2南方基金12535.71
3广发基金12398.20
4汇添富基金11204.00
# 取排名前10的基金公司进行展示
# orient='h': 设置条形图为水平方向
sns.barplot(x = '基金规模(亿元)',
            y = '公司',
            data = com.iloc[:10],
            orient='h')

plt.savefig('./十大基金公司.png',dpi = 200)

在这里插入图片描述

收益十佳基金经理

import numpy as np

fund = pd.read_excel('./fund_result.xlsx')

# 降序排序
fund.sort_values(by = '基金收益(%)',ascending=False, inplace=True) 
fund.iloc[:10]

姓名公司基金数量基金规模(亿元)基金收益(%)
2179朱少醒富国基金215148306.812082.04
1802杨谷诺安基金1155034.82739.50
1769谢治宇兴证全球基金4872597.58710.91
2083张坤易方达基金481951197.46681.80
48陈军东吴基金21328834.63554.64
1370魏博中欧基金5823920.15516.85
88曹名长中欧基金81411154.68501.94
1698萧楠易方达基金68195626.39501.19
1287孙伟民生加银基金126278233.79494.82
1220孙芳上投摩根基金5912599.72488.85
plt.figure(figsize=(12,9))
sns.set_theme(style='darkgrid',context = 'talk', font='SimHei')

# 条形图
# palette 指定使用'Set1'调色板
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)) # 横坐标刻度
plt.savefig('./收益十佳基金经理.png', dpi = 100)

在这里插入图片描述

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值