Python数据分析实例操作

简介: import pandas as pd #导入pandasimport matplotlib.pyplot as plt #导入matplotlibfrom pylab import *mpl.
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

知识在于点滴积累
目录
相关文章
|
1天前
|
JSON 数据格式 索引
python 又一个点运算符操作的字典库:Munch
python 又一个点运算符操作的字典库:Munch
10 0
|
6天前
|
索引 Python
如何使用Python的Pandas库进行数据透视表(pivot table)操作?
使用Pandas在Python中创建数据透视表的步骤包括:安装Pandas库,导入它,创建或读取数据(如DataFrame),使用`pd.pivot_table()`指定数据框、行索引、列索引和值,计算聚合函数(如平均分),并可打印或保存结果到文件。这允许对数据进行高效汇总和分析。
10 2
|
6天前
|
机器学习/深度学习 数据挖掘 计算机视觉
python数据分析工具SciPy
【4月更文挑战第15天】SciPy是Python的开源库,用于数学、科学和工程计算,基于NumPy扩展了优化、线性代数、积分、插值、特殊函数、信号处理、图像处理和常微分方程求解等功能。它包含优化、线性代数、积分、信号和图像处理等多个模块。通过SciPy,可以方便地执行各种科学计算任务。例如,计算高斯分布的PDF,需要结合NumPy使用。要安装SciPy,可以使用`pip install scipy`命令。这个库极大地丰富了Python在科学计算领域的应用。
11 1
|
7天前
|
数据可视化 数据挖掘 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() ```
12 1
|
7天前
|
数据采集 SQL 数据可视化
Python数据分析工具Pandas
【4月更文挑战第14天】Pandas是Python的数据分析库,提供Series和DataFrame数据结构,用于高效处理标记数据。它支持从多种数据源加载数据,包括CSV、Excel和SQL。功能包括数据清洗(处理缺失值、异常值)、数据操作(切片、过滤、分组)、时间序列分析及与Matplotlib等库集成进行数据可视化。其高性能底层基于NumPy,适合大型数据集处理。通过加载数据、清洗、分析和可视化,Pandas简化了数据分析流程。广泛的学习资源使其成为数据分析初学者的理想选择。
13 1
|
7天前
|
存储 机器学习/深度学习 数据可视化
Python面板时间序列数据预测:格兰杰因果关系检验Granger causality test药品销售实例与可视化
Python面板时间序列数据预测:格兰杰因果关系检验Granger causality test药品销售实例与可视化
55 6
|
7天前
|
机器学习/深度学习 数据可视化 算法
PYTHON用决策树分类预测糖尿病和可视化实例
PYTHON用决策树分类预测糖尿病和可视化实例
16 0
|
7天前
|
算法 数据可视化 Python
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
Python中LARS和Lasso回归之最小角算法Lars分析波士顿住房数据实例
13 0
|
9天前
|
Python 数据挖掘 存储
Python 数据分析(PYDA)第三版(七)(4)
Python 数据分析(PYDA)第三版(七)
33 1
|
Python Shell 存储
Python 数据分析(PYDA)第三版(七)(3)
Python 数据分析(PYDA)第三版(七)
44 1
Python 数据分析(PYDA)第三版(七)(3)

热门文章

最新文章