当前位置:   article > 正文

python数据分析简单实例-Python数据分析实例操作

python数据分析简单案例

import pandas as pd #导入pandas

import matplotlib.pyplot as plt #导入matplotlib

from pylab import *

mpl.rcParams['font.sans-serif'] = ['SimHei']

mpl.rcParams['axes.unicode_minus'] = False

%matplotlib inline

数据读取与索引

bra = pd.read_csv('data/bra.csv')

bra.head()

img_13fb5f7479601db9e3451d6985cce691.png

选取列

bra.content

img_d3fb6598675094232a238a71ebed029e.png

bra[['creationTime','productColor']].head()

img_a25bdd17bf17ae171d9f0a4e93b77d5f.png

选择行

bra[1:6]

img_e24c548d4903d9445d3161521156ffb4.png

选择行和列

bra.ix[[2,3],[1,3]] #使用ix

img_525b89aa19fece137451c8d21d3acbd2.png

bra.ix[1:5,['productColor']]

img_739ec1279a71d9db4d7fbf41fbec4695.png

bra.iloc[[2,3],[1,3]] #使用iloc

img_1fa82c81b526a9ad0885c5c462aedad1.png

bra.loc[1:5,['content','creationTime','productSize']] #使用loc

img_be167d03c15a6868e93e8b025ecc3422.png

bra.loc[1:5,'content':'userClientShow']

img_8172c81539e0124494c219cfd0f4bda7.png

数据预处理

缺失值

bra.describe() #查看数据的分布情况,可返回变量和观测的数量、缺失值和唯一值的数目、平均值、分位数等相关信息

img_7da23b9b5c1d20b87ca5b8789bd8f886.png

bra['userClientShow'].unique() #userClientShow列有几种选项

img_780f280b82d6f7d243900a90b5e54296.png

bra['userClientShow'].isnull().sum() #初始缺失值数量

img_35330d53f13b354f49a82fa3d848d4c6.png

bra['userClientShow'].fillna('不详',inplace=True) #缺失值替换为"不详”

bra['userClientShow'].isnull().sum() #赋值后的缺失值数量

img_27c2d2681bb90415dce5c971d9cca609.png

新增列

bra.dtypes #查看属性

img_4a01147fd52e2f9501f986b75651facd.png

bra['creationTime'] = pd.to_datetime(bra['creationTime']) #更新类型

bra.dtypes

img_10ecb79f42afaba32514523dc49b3844.png

bra['hour'] = [i.hour for i in bra['creationTime']] #新建hour列

bra

img_b291940f86159807cb2267ef629dd872.png

字符串操作

bra.productSize.unique() #查看productSize的唯一值

img_2ef04831ff7efc343c1d15a0c0513436.png

cup = bra.productSize.str.findall('[a-zA-Z]+').str[0] #新增列cup

cup2 = cup.str.replace('M','B')

cup3 = cup2.str.replace('L','C')

cup4 = cup3.str.replace('XC','D')

bra['cup'] = cup4

bra.head()

img_9d2e9cace35cbf25d1b317e3de728705.png

bra['cup'].unique() #查看cup唯一值

img_5e8d2fab7f8a127fc6df20949ea472c9.png

数据转换

bra.productColor.unique() #查看productColor唯一值

img_37a3615c6f902a3ba58419a647172c81.png

def getColor(s):

if '黑' in s:

return '黑色'

elif '肤' in s:

return '肤色'

elif '蓝' in s:

return '蓝色'

elif '红' in s:

return '红色'

elif '紫' in s:

return '紫色'

elif '白' in s:

return '白色'

elif '粉' in s:

return '粉色'

elif '灰' in s:

return '灰色'

elif '绿' in s:

return '绿色'

elif '青' in s:

return '青色'

else:

return s

bra['color'] = bra['productColor'].map(getColor) #从productColor列查询,赋值到定义的函数getColor,最终新增列color

bra

img_cd8940a9aba0c60af6a281842297a4a5.png

bra.color.unique() #查询color的唯一值

img_7ee15f25c28a09ff5be2db824dacc7d2.png

数据可视化

x = [1991,1992,1993,1994,1995,1996,1997]

y = [23,56,38,29,34,56,92]

plt.plot(x,y) #调用函数plot

img_598c836897e06bf6d9ca0e75a3fc9bc4.png

plt.figure(figsize=(8,6),dpi=80) #调用函数firgure

plt.plot(x,y)

img_aaba207b291d0443db93828cabb4bcca.png

hour = bra.groupby('hour')['hour'].count() #hour列排序

hour

img_f9c5c0e360aa1f78214240f362501f22.png

plt.xlim(0,25) #横轴0~25

plt.plot(hour,linestyle='solid',color='royalblue',marker='8') #颜色深蓝

img_b68758302e6b9b3bd98b2aac45f2c361.png

cup_style = bra.groupby('cup')['cup'].count() #cup列唯一值得数量

cup_style

img_0a94c2d34687ecbb62c0d8e13876b71f.png

plt.figure(figsize=(8,6),dpi=80)

labels = list(cup_style.index)

plt.xlabel('cup') #x轴为cup

plt.ylabel('count') #y轴为count数量

plt.bar(range(len(labels)),cup_style,color='royalblue',alpha=0.7) #alpha为透明度

plt.xticks(range(len(labels)),labels,fontsize=12)

plt.grid(color='#95a5a6',linestyle='--',linewidth=1,axis='y',alpha=0.6)

plt.legend(['user-count'])

for x,y in zip(range(len(labels)),cup_style):

plt.text(x,y,y,ha='center',va='bottom')

img_61a5a140de9b2178449381c5025fe196.png

color_style = bra.groupby('color')['color'].count() #color列唯一值得数量

color_style

img_fc4685880bb23f481c243e445e758e38.png

plt.figure(figsize=(8,6),dpi=80)

plt.subplot(facecolor='gainsboro',alpha=0.2)

colors = ['brown','orange','gray','white','pink','purple','red','green','wheat','blue','gold','springgreen','black'] #颜色种类

labels = list(color_style.index)

plt.xlabel('count') #x轴为count数量

plt.ylabel('color') #y轴为color

plt.title('Color Distribution') #定义标题

plt.barh(range(len(labels)),color_style,color=colors,alpha=1)

plt.yticks(range(len(labels)),labels,fontsize=12)

plt.grid(color='#95a5a6',linestyle='--',linewidth=1,axis='x',alpha=0.4)

img_52fa8d3d9de5c64fd22f099c9fbfdcf4.png

bra.head(30)

img_30d1abcadb99b329852d2a923f3a048c.png

知识在于点滴积累

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/803173
推荐阅读
相关标签
  

闽ICP备14008679号