代码
import os
import matplotlib.pyplot as plt
import pandas as pd
user_device_file = '/Users/miraco/PycharmProjects/DataMining/data_pd/mobile_data/user_device.csv'
user_usage_file = '/Users/miraco/PycharmProjects/DataMining/data_pd/mobile_data/user_usage.csv'
output_path = '/Users/miraco/PycharmProjects/DataMining/output3'
#数据获取
user_device_df = pd.read_csv(user_device_file)
user_usage_df = pd.read_csv(user_usage_file)
#字符串列合并,把platform,platform_version两列合并,但是有一列是数字,要转换成字符
user_device_df['platform_version'] = user_device_df['platform_version'].astype('str')
user_device_df['system'] = user_device_df['platform'].str.cat(user_device_df['platform_version'],sep = '_')
#合并数据集
merged_df = pd.merge(user_device_df, user_usage_df, how = 'inner', on = 'user_id')
#数据分析
#按照系统查看每月流量的使用情况
system_usage_ser = merged_df.groupby('system')['monthly_mb'].mean()
#降序排列,自身接收自身的结果(不想创建新变量)
system_usage_ser.sort_values(ascending=False, inplace = True)
system_usage_ser.to_csv(os.path.join(output_path,'mobile_system_usage.csv'))
system_usage_ser.plot(kind = 'bar', rot = 45)
plt.ylabel('Monthly User (MB)')
plt.tight_layout()
plt.savefig(os.path.join(output_path,'mobile_system_usage.png'))
plt.show()
运行出图
练习:统计不同专业的员工平均薪资
题目描述:统计不同专业背景的员工的平均薪资,并用柱状图显示结果
题目要求:
使用Pandas进行数据分析及可视化
数据文件:
数据源下载地址:https://video.mugglecode.com/data_employee.zip,下载压缩包后解压即可
employee_info.csv,包含了员工的基本信息,共4列数据,分别表示:
- EmployeeNumber: 员工编号
- Age: 年龄
- Department: 所处部门
- MonthlyIncome: 月收入
- employee_edu.csv,包含部分员工的专业背景,共2列数据:
- EmployeeNumber: 员工编号
- EducationField: 专业背景
import os
import matplotlib.pyplot as plt
import pandas as pd
employee_edu_file = '/Users/miraco/PycharmProjects/DataMining/data_pd/data_employee/employee_edu.csv'
employee_info_file = '/Users/miraco/PycharmProjects/DataMining/data_pd/data_employee/employee_info.csv'
output_path = '/Users/miraco/PycharmProjects/DataMining/output3'
#数据获取
employee_edu_df = pd.read_csv(employee_edu_file)
employee_info_df = pd.read_csv(employee_info_file)
#数据处理
merged_df = pd.merge(employee_edu_df, employee_info_df, how = 'inner',on = 'EmployeeNumber')
#数据分析
seq = merged_df.groupby('EducationField')['MonthlyIncome'].mean().sort_values()
#数据展示
seq.plot(kind = 'bar', rot = 45)
plt.ylabel('Monthly Income')
plt.xlabel('Education Field')
plt.tight_layout()
plt.show()
运行结果