|NO.Z.00043|——————————|BigDataEnd|——|Hadoop&Python.v07|——|Arithmetic.v07|Pandas数据分析库:Pandas数据清洗|

一、数据清洗
### --- 数据清洗

import numpy as np
import pandas as pd
df = pd.DataFrame(data = {'color':
['red','blue','red','green','blue',None,'red'],
                          'price':[10,20,10,15,20,0,np.NaN]})
# 1、重复数据过滤
df.duplicated()                                                     # 判断是否存在重复数据
df.drop_duplicates()                                                # 删除重复数据

# 2、空数据过滤
df.isnull()                                                         # 判断是否存在空数据,存在返回True,否则返回False
df.dropna(how = 'any')                                              # 删除空数据
df.fillna(value=1111)                                               # 填充空数据

# 3、指定⾏或者列过滤
del df['color']                                                     # 直接删除某列
df.drop(labels = ['price'],axis = 1)                                # 删除指定列
df.drop(labels = [0,1,5],axis = 0)                                  # 删除指定⾏

# 4、函数filter使⽤
df = pd.DataFrame(np.array(([3,7,1], [2, 8, 256])),
                  index=['dog', 'cat'],
                  columns=['China', 'America', 'France'])
df.filter(items=['China', 'France'])
# 根据正则表达式删选列标签
df.filter(regex='a$', axis=1)
# 选择⾏中包含og
df.filter(like='og', axis=0)

# 5、异常值过滤
df2 = pd.DataFrame(data = np.random.randn(10000,3))                 # 正态分布数据
# 3σ过滤异常值,σ即是标准差
cond = (df2 > 3*df2.std()).any(axis = 1)
index = df2[cond].index                                             # 不满⾜条件的⾏索引
df2.drop(labels=index,axis = 0)                                     # 根据⾏索引,进⾏数据删除

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(8)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示