缺失值處理
import pandas as pd
# 讀取杭州天氣文件
df = pd.read_csv("hz_weather.csv")
# 數(shù)據(jù)透視表
df1 = pd.pivot_table(df, index=['天氣'], columns=['風向'], values=['最高氣溫'])
print(df.head())
print(df1.head())
# 用isnull()獲得缺失值位置為True,非缺失值位置為False的DataFrame
lack = df1.isnull()
print(lack)
# 用any()可以看到哪些列有缺失值
lack_col = lack.any()
print(lack_col)
# 顯示存在缺失值的行列
df1_lack_only = df1[df1.isnull().values == True]
print(df1_lack_only
# 刪除缺失的行
df1_del_lack_row = df1.dropna(axis=0)
print(df1_del_lack_row)
# 刪除缺失的列(一般不因為某列有缺失值就刪除列, 因為列常代表某指標)
df1_del_lack_col = df1.dropna(axis=1)
print(df1_del_lack_col) # 只剩下北風
# 使用字符串代替缺失值
df1_fill_lcak1 = df1.fillna('missing')
print(df1_fill_lcak1)
# 使用前一個數(shù)據(jù)(同列的上一個數(shù)據(jù))替代缺失值,第一行的缺失值沒法找到替代值
df1_fill_lack2 = df1.fillna(method='pad')
print(df1_fill_lack2)
# 使用后一個數(shù)據(jù)(同列的下一個數(shù)據(jù))替代缺失值,最后一行的缺失值沒法找到替代值
# 參數(shù)limit=1限制每列最多只能替代掉一個NaN
df1_fill_lack3 = df1.fillna(method='bfill', limit=1)
print(df1_fill_lack3)
# df對象的mean()方法會求每一列的平均值,也就是每個指標的平均值.下面使用平均數(shù)代替NaN
df1_fill_lack4 = df1.fillna(df1.mean())
print(df1_fill_lack4)
檢測異常值
檢測異常值的方法:https://blog.csdn.net/qianfeng_dashuju/article/details/81708597
假設(shè)"最低氣溫"是符合正態(tài)分布的先朦,那么就可以根據(jù)3σ原則,認為落在[?3σ+μ,+3σ+μ]之外的值是異常值
# %matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
# 讀取杭州天氣數(shù)據(jù)
df = pd.read_csv("hz_weather.csv")
# 創(chuàng)建圖的布局,位于1行1列,寬度為8,高度為5,這兩個指標*dpi=像素值,dpi默認為80(保存圖像時為100)
# 返回的fig是繪圖窗口,ax是坐標系
fig, ax = plt.subplots(1, 1, figsize=(8, 5))
# hist函數(shù)繪制柱狀圖,第一個參數(shù)傳入數(shù)值序列(這里是Series),這里即是最低氣溫.bins指定有多少個柱子
ax.hist(df['最低氣溫'], bins=20)
# 顯示圖
plt.show()
# 取最低氣溫一列,得到的是Series對象
s = df['最低氣溫']
# 計算到miu的距離(還沒取絕對值)
zscore = s - s.mean()
# 標準差sigma
sigma = s.std()
# 添加一列,記錄是否是異常值,如果>3倍sigma就認為是異常值
df['isOutlier'] = zscore.abs() > 3 * sigma
# 計算異常值數(shù)目,也就是這一列中值為True的數(shù)目
print(df['isOutlier'].value_counts())
四分位數(shù)作箱型圖和檢測異常值
p分位數(shù)概念:https://blog.csdn.net/u011327333/article/details/71263081?locationNum=14&fps=1
# %matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
# 符合格式的txt文件也可以直接當csv文件讀入
df = pd.read_csv('sale_data.txt')
# 創(chuàng)建圖布局
fig, ax = plt.subplots(1, 1, figsize=(8, 5))
# 取上海數(shù)據(jù)
df_ = df[df['位置'] == '上海']
# 函數(shù)boxplot用于繪制箱型圖,繪制的指標是'成交量',坐標用前面matplotlib創(chuàng)建的坐標系
df_.boxplot(column='成交量', ax=ax)
plt.show()
# 查看上海的成交量情況,這里即提取為Series對象
s = df_['成交量']
print(s.describe())
# 這里規(guī)避A value is trying to be set on a copy of a slice from a DataFrame
df_ = df_.copy()
# 這里將大于上四分位數(shù)(Q3)的設(shè)定為異常值
# df_['isOutlier'] = s > s.quantile(0.75)
df_.loc[:, 'isOutlier'] = s > s.quantile(0.75)
# 查看上海成交量異常的數(shù)據(jù)
df_rst = df_[df_['isOutlier'] == True]
print(df_rst)
重復(fù)值處理
import pandas as pd
# 讀取杭州天氣數(shù)據(jù)
df = pd.read_csv('E:/Data/practice/hz_weather.csv')
# 檢測重復(fù)行,生成bool的DF
s_isdup = df.duplicated()
# print(s_isdup)
print(s_isdup.value_counts()) # 全是False
# 檢測最高氣溫重復(fù)的行
s_isdup_zgqw = df.duplicated('最高氣溫')
print(s_isdup_zgqw.value_counts())
# 去除'最高氣溫'重復(fù)的行
df_dup_zgqw = df.drop_duplicates('最高氣溫')
# print(df_dup_zgqw)