Pandas 数据清洗

前言:

数据的质量直接关乎最后数据分析出来的结果。

在进行数据分析前,我们必须对数据进行清洗。

如果数据较少,我们可以对缺失值,异常值进行拉格朗日插值法进行插值处理。

在这篇博客里,因为数据样本较充足,我们直接对少量的异常值进行简单粗暴也是最有效的删除。


先上图看一下数据(注:因为博客上传不了附件,如有需要样本数据自己进行实测的可以留言索要)

Pandas 数据清洗_第1张图片

上图为要进行清洗的数据的一部分。

这里,我们首先用Pandas直接读入数据变成DataFrame.(Pandas库不懂的同学要去看看《Python数据分析》)

import pandas as pd 
import numpy as np

# 读入数据
data=pd.DataFrame(pd.read_excel('000.xlsx',index=False))
print(data)

打印出数据,打印出原始数据如下图所示:

Pandas 数据清洗_第2张图片

目标:

这里,我们可以看到,日期列没什么问题,时间列(hour)出现235小时明显不符,一天时间只能落在0-24。气压(pressure)超过1500不正常。sea_pressure有存在空值现象。wind_direction也存在值=999017的异常数据。我们要做的是对存在异常值的记录整行进行删除,最后写入数据。


我们首先利用describe()函数看样本数据的大概情况。

print(data.describe())

Pandas 数据清洗_第3张图片


count非空数据总和。可以看到 sea_pressure 的非空数据总和为857,rel_humidity为861,证明这两列数据分别存在5,1行空值(缺失值)

mean均值

std标准差,值越大表示此行数据越离散。存在异常值概率越大。

min,max:最小值最大值。时间hour列最大值为235明显为异常值。


开始数据清洗

1 填充缺失值空格,方便下面对缺失值行进行处理。

# 填充缺失值
data=data.fillna('missing')

我们可以看到,缺失值被填充成‘missing’,填充成什么值不重要,只要不予数据里的其它值一样就行。

Pandas 数据清洗_第4张图片


2  逐行扫描数据,只要此行存在一个异常值。对此行进行删除。

这里,any([])函数如若里面一个为真,则返回True.即逐行扫描,发现‘missing’存在此行中,或此行时间不落在0-24之间,或气压rpessure大于1500,或风向不落在0-360度之间,或风速大于10级,或precipitation降雨量大于10,则此行数据为异常数据进行删除。

for i in range(data.index.max()):
	if any([
		'missing' in data.loc[i,:].values,
		data.loc[i,'hour'] not in range(25),
		data.loc[i,'pressure']>1500,
		data.loc[i,'wind_direction']<0 or data.loc[i,'wind_direction']>360,
		data.loc[i,'wind_speed']>10,
		data.loc[i,'precipitation']>10
		]):

		print('已删除存在异常值 %s 行数据'%i)
		data.drop([i],inplace=True)

运行结果:

Pandas 数据清洗_第5张图片

3显示处理后数据信息:

 

print(data)
print(data.describe())
Pandas 数据清洗_第6张图片

Pandas 数据清洗_第7张图片

4最后,查看无异常后保存清洗后的数据。

data.to_csv('clean.csv')



全部代码:

import pandas as pd 
import numpy as np

# 读入数据
data=pd.DataFrame(pd.read_excel('000.xlsx',index=False))
print('原始数据\n',data)
print('----------------------------基本信息  describe()-----------------------------')
print(data.describe())
# print(data.std(),data.min(),data.max())

'''
print('\n-----------------------极差变异系数四分位间距-------------------------')
statistics = data.describe() #保存基本统计量
statistics.loc['range'] = statistics.loc['max']-statistics.loc['min'] #极差
statistics.loc['var'] = statistics.loc['std']/statistics.loc['mean'] #变异系数
statistics.loc['dis'] = statistics.loc['75%']-statistics.loc['25%'] #四分位数间距
# print(statistics)
'''



print('-------------------------------数据清洗-------------------------------')
# 填充缺失值
data=data.fillna('missing')
for i in range(data.index.max()):
	if any([
		'missing' in data.loc[i,:].values,
		data.loc[i,'hour'] not in range(25),
		data.loc[i,'pressure']>1500,
		data.loc[i,'wind_direction']<0 or data.loc[i,'wind_direction']>360,
		data.loc[i,'wind_speed']>10,
		data.loc[i,'precipitation']>10
		]):

		print('已删除存在异常值 %s 行数据'%i)
		data.drop([i],inplace=True)
print('清洗后数据\n',data)
print(data.describe())
data.to_csv('clean.csv')


运行结果:

原始数据
           date  hour  pressure  sea_pressure  wind_direction  wind_speed  \
0   2016-07-01     0    1000.4           NaN             225         2.1   
1   2016-07-01     3    2000.5           NaN             187         2.7   
2   2016-07-01     6     998.9           NaN             212         2.9   
3   2016-07-01   235     998.7           NaN             244         2.7   
4   2016-07-01    12     999.7           NaN             222         1.3   
5   2016-07-01    15    1000.0        1004.8             102         1.6   
6   2016-07-01    18     998.8        1003.6             202         1.9   
7   2016-07-01    21    1000.2        1005.0             334         2.2   
8   2016-07-02     0    1001.6        1006.4             315         1.8   
9   2016-07-02     3    1002.4        1007.2              46         3.2   
10  2016-07-02     6    1001.3        1006.2              37         2.2   
11  2016-07-02     9    1001.9        1006.8             345         2.4   
12  2016-07-02    12    1003.6        1008.5             113         1.0   
13  2016-07-02    15    1002.4        1007.3             138         1.0   
14  2016-07-02    18    1000.9        1005.8             221         0.7   
15  2016-07-02    21    1000.9        1005.8          999017         0.1   
16  2016-07-03     0    1001.5        1006.4             220         1.5   
17  2016-07-03     3    1001.5        1006.4             200         1.6   
18  2016-07-03     6    1000.8        1005.7             207         1.8   
19  2016-07-03     9    1000.8        1005.7             223         1.1   
20  2016-07-03    12    1001.6        1006.5             137         0.6   
21  2016-07-03    15    1002.2        1007.1             173         0.4   
22  2016-07-03    18    1000.5        1005.4             330         0.7   
23  2016-07-03    21    1000.4        1005.3          999017         0.0   
24  2016-07-04     0    1000.6        1005.5             176         2.0   
25  2016-07-04     3    1000.5        1005.3             179         2.1   
26  2016-07-04     6     999.7        1004.5             160         1.7   
27  2016-07-04     9     998.8        1003.6             287         1.4   
28  2016-07-04    12    1001.1        1006.0             340         1.0   
29  2016-07-04    15    1001.1        1005.9             188         1.3   
..         ...   ...       ...           ...             ...         ...   
832 2016-10-14     6    1015.8        1020.9             359         1.9   
833 2016-10-14     9    1015.5        1020.6             349         2.0   
834 2016-10-14    12    1016.5        1021.6               1         1.4   
835 2016-10-14    15    1015.9        1021.0             354         1.8   
836 2016-10-14    18    1015.4        1020.5             202         1.1   
837 2016-10-14    21    1015.2        1020.3             360         2.3   
838 2016-10-15     0    1016.5        1021.6              35         2.0   
839 2016-10-15     3    1015.8        1020.9             357         1.1   
840 2016-10-15     6    1013.7        1018.8             334         1.5   
841 2016-10-15     9    1013.7        1018.8             333         2.2   
842 2016-10-15    12    1014.5        1019.6             344         0.9   
843 2016-10-15    15    1013.9        1019.0          999017         0.0   
844 2016-10-15    18    1012.9        1017.9             215         1.7   
845 2016-10-15    21    1012.8        1017.8             216         3.2   
846 2016-10-16     0    1013.9        1018.9             327         0.6   
847 2016-10-16     3    1013.9        1018.9             342         2.2   
848 2016-10-16     6    1011.7        1016.7             301         1.8   
849 2016-10-16     9    1011.7        1016.7              10         4.1   
850 2016-10-16    12    1013.8        1018.8             353         4.3   
851 2016-10-16    15    1014.7        1019.7               7         2.1   
852 2016-10-16    18    1014.5        1019.5             344         2.0   
853 2016-10-16    21    1014.2        1019.2              35         3.0   
854 2016-10-17     0    1015.6        1020.6              35         2.1   
855 2016-10-17     3    1015.6        1020.6             104         1.5   
856 2016-10-17     6    1013.4        1018.4              81         1.2   
857 2016-10-17     9    1013.7        1018.7             109         1.1   
858 2016-10-17    12    1014.3        1019.4              17         0.7   
859 2016-10-17    15    1014.7        1019.8             222         0.9   
860 2016-10-17    18    1014.0        1019.0             341         1.5   
861 2016-10-17    21    1013.9        1018.9             322         2.5   

     temperature  rel_humidity  precipitation  
0           26.4           NaN            0.0  
1           29.0          76.0            0.0  
2           31.7          67.0            0.0  
3           31.6          59.0            0.0  
4           29.9          68.0            0.0  
5           27.8          82.0            0.0  
6           26.0          89.0            0.0  
7           25.5          90.0            0.0  
8           26.8          82.0            0.0  
9           30.0          70.0            0.0  
10          29.3          80.0            1.0  
11          25.9          95.0           10.2  
12          25.1          94.0            0.2  
13          25.3          96.0            0.0  
14          25.4          90.0            0.0  
15          25.2          98.0            0.0  
16          25.8          97.0            0.0  
17          27.0          93.0            0.1  
18          27.4          92.0            0.0  
19          27.7          91.0            0.1  
20          26.9          91.0            0.0  
21          26.0          96.0            0.1  
22          25.7          98.0            0.4  
23          25.7          98.0            0.0  
24          26.3          97.0            0.0  
25          31.8          69.0            0.0  
26          34.7          56.0            0.0  
27          34.8          55.0            0.0  
28          29.7          78.0            0.0  
29          27.8          79.0            0.0  
..           ...           ...            ...  
832         18.5          84.0            0.0  
833         18.2          81.0            0.0  
834         18.3          83.0            0.0  
835         18.2          87.0            0.0  
836         18.4          89.0            0.0  
837         18.5          90.0            0.0  
838         19.0          95.0            0.0  
839         19.8          94.0            0.6  
840         20.3          96.0            0.1  
841         20.3          96.0            0.0  
842         20.0          97.0            0.0  
843         19.8          97.0            0.0  
844         19.7          97.0            0.0  
845         19.6          97.0            0.0  
846         20.4          95.0            0.0  
847         24.6          76.0            0.0  
848         26.8          67.0            0.0  
849         24.9          74.0            0.0  
850         22.7          77.0            0.0  
851         21.9          80.0            0.0  
852         21.3          83.0            0.0  
853         20.4          81.0            0.0  
854         20.1          80.0            0.0  
855         19.6          90.0            0.1  
856         21.3          79.0            0.0  
857         20.9          79.0            0.0  
858         19.6          87.0            0.0  
859         18.9          92.0            0.0  
860         19.1          92.0            0.0  
861         19.3          90.0            0.0  

[862 rows x 9 columns]
----------------------------基本信息  describe()-----------------------------
             hour     pressure  sea_pressure  wind_direction  wind_speed  \
count  862.000000   862.000000    857.000000      862.000000  862.000000   
mean    10.762181  1005.149768   1008.892649     9449.430394    2.302552   
std     10.277353    34.375581      5.536994    95832.730719    1.215748   
min      0.000000   993.400000    998.200000        0.000000    0.000000   
25%      6.000000  1000.000000   1004.800000       81.000000    1.500000   
50%     12.000000  1002.800000   1007.600000      188.000000    2.000000   
75%     17.250000  1007.000000   1011.900000      295.000000    2.900000   
max    235.000000  2000.500000   1023.500000   999017.000000    7.500000   

       temperature  rel_humidity  precipitation  
count   862.000000    861.000000     862.000000  
mean     27.400348     73.844367       0.212529  
std       4.869077     15.886997       1.385508  
min      14.100000     25.000000       0.000000  
25%      23.800000     63.000000       0.000000  
50%      27.400000     75.000000       0.000000  
75%      30.800000     87.000000       0.000000  
max      39.400000     98.000000      27.200000  
-------------------------------数据清洗-------------------------------
已删除存在异常值 0 行数据
已删除存在异常值 1 行数据
已删除存在异常值 2 行数据
已删除存在异常值 3 行数据
已删除存在异常值 4 行数据
已删除存在异常值 11 行数据
已删除存在异常值 15 行数据
已删除存在异常值 23 行数据
已删除存在异常值 84 行数据
已删除存在异常值 271 行数据
已删除存在异常值 283 行数据
已删除存在异常值 290 行数据
已删除存在异常值 312 行数据
已删除存在异常值 411 行数据
已删除存在异常值 449 行数据
已删除存在异常值 734 行数据
已删除存在异常值 843 行数据
清洗后数据
           date  hour  pressure sea_pressure  wind_direction  wind_speed  \
5   2016-07-01    15    1000.0       1004.8             102         1.6   
6   2016-07-01    18     998.8       1003.6             202         1.9   
7   2016-07-01    21    1000.2         1005             334         2.2   
8   2016-07-02     0    1001.6       1006.4             315         1.8   
9   2016-07-02     3    1002.4       1007.2              46         3.2   
10  2016-07-02     6    1001.3       1006.2              37         2.2   
12  2016-07-02    12    1003.6       1008.5             113         1.0   
13  2016-07-02    15    1002.4       1007.3             138         1.0   
14  2016-07-02    18    1000.9       1005.8             221         0.7   
16  2016-07-03     0    1001.5       1006.4             220         1.5   
17  2016-07-03     3    1001.5       1006.4             200         1.6   
18  2016-07-03     6    1000.8       1005.7             207         1.8   
19  2016-07-03     9    1000.8       1005.7             223         1.1   
20  2016-07-03    12    1001.6       1006.5             137         0.6   
21  2016-07-03    15    1002.2       1007.1             173         0.4   
22  2016-07-03    18    1000.5       1005.4             330         0.7   
24  2016-07-04     0    1000.6       1005.5             176         2.0   
25  2016-07-04     3    1000.5       1005.3             179         2.1   
26  2016-07-04     6     999.7       1004.5             160         1.7   
27  2016-07-04     9     998.8       1003.6             287         1.4   
28  2016-07-04    12    1001.1         1006             340         1.0   
29  2016-07-04    15    1001.1       1005.9             188         1.3   
30  2016-07-04    18    1000.8       1005.6             188         2.2   
31  2016-07-04    21    1002.8       1007.6             261         0.3   
32  2016-07-05     0    1003.9       1008.7             205         2.0   
33  2016-07-05     3    1004.7       1009.5             122         1.2   
34  2016-07-05     6    1003.7       1008.5             192         3.2   
35  2016-07-05     9    1004.4       1009.3             334         5.3   
36  2016-07-05    12    1006.7       1011.6             287         0.7   
37  2016-07-05    15    1006.8       1011.7             224         2.0   
..         ...   ...       ...          ...             ...         ...   
831 2016-10-14     3    1017.8       1022.9               2         1.6   
832 2016-10-14     6    1015.8       1020.9             359         1.9   
833 2016-10-14     9    1015.5       1020.6             349         2.0   
834 2016-10-14    12    1016.5       1021.6               1         1.4   
835 2016-10-14    15    1015.9         1021             354         1.8   
836 2016-10-14    18    1015.4       1020.5             202         1.1   
837 2016-10-14    21    1015.2       1020.3             360         2.3   
838 2016-10-15     0    1016.5       1021.6              35         2.0   
839 2016-10-15     3    1015.8       1020.9             357         1.1   
840 2016-10-15     6    1013.7       1018.8             334         1.5   
841 2016-10-15     9    1013.7       1018.8             333         2.2   
842 2016-10-15    12    1014.5       1019.6             344         0.9   
844 2016-10-15    18    1012.9       1017.9             215         1.7   
845 2016-10-15    21    1012.8       1017.8             216         3.2   
846 2016-10-16     0    1013.9       1018.9             327         0.6   
847 2016-10-16     3    1013.9       1018.9             342         2.2   
848 2016-10-16     6    1011.7       1016.7             301         1.8   
849 2016-10-16     9    1011.7       1016.7              10         4.1   
850 2016-10-16    12    1013.8       1018.8             353         4.3   
851 2016-10-16    15    1014.7       1019.7               7         2.1   
852 2016-10-16    18    1014.5       1019.5             344         2.0   
853 2016-10-16    21    1014.2       1019.2              35         3.0   
854 2016-10-17     0    1015.6       1020.6              35         2.1   
855 2016-10-17     3    1015.6       1020.6             104         1.5   
856 2016-10-17     6    1013.4       1018.4              81         1.2   
857 2016-10-17     9    1013.7       1018.7             109         1.1   
858 2016-10-17    12    1014.3       1019.4              17         0.7   
859 2016-10-17    15    1014.7       1019.8             222         0.9   
860 2016-10-17    18    1014.0         1019             341         1.5   
861 2016-10-17    21    1013.9       1018.9             322         2.5   

     temperature rel_humidity  precipitation  
5           27.8           82            0.0  
6           26.0           89            0.0  
7           25.5           90            0.0  
8           26.8           82            0.0  
9           30.0           70            0.0  
10          29.3           80            1.0  
12          25.1           94            0.2  
13          25.3           96            0.0  
14          25.4           90            0.0  
16          25.8           97            0.0  
17          27.0           93            0.1  
18          27.4           92            0.0  
19          27.7           91            0.1  
20          26.9           91            0.0  
21          26.0           96            0.1  
22          25.7           98            0.4  
24          26.3           97            0.0  
25          31.8           69            0.0  
26          34.7           56            0.0  
27          34.8           55            0.0  
28          29.7           78            0.0  
29          27.8           79            0.0  
30          27.7           80            0.0  
31          26.5           87            0.0  
32          28.9           78            0.0  
33          33.2           62            0.0  
34          34.8           53            0.0  
35          28.7           74            0.0  
36          25.7           91            0.7  
37          25.0           93            0.7  
..           ...          ...            ...  
831         18.8           82            0.0  
832         18.5           84            0.0  
833         18.2           81            0.0  
834         18.3           83            0.0  
835         18.2           87            0.0  
836         18.4           89            0.0  
837         18.5           90            0.0  
838         19.0           95            0.0  
839         19.8           94            0.6  
840         20.3           96            0.1  
841         20.3           96            0.0  
842         20.0           97            0.0  
844         19.7           97            0.0  
845         19.6           97            0.0  
846         20.4           95            0.0  
847         24.6           76            0.0  
848         26.8           67            0.0  
849         24.9           74            0.0  
850         22.7           77            0.0  
851         21.9           80            0.0  
852         21.3           83            0.0  
853         20.4           81            0.0  
854         20.1           80            0.0  
855         19.6           90            0.1  
856         21.3           79            0.0  
857         20.9           79            0.0  
858         19.6           87            0.0  
859         18.9           92            0.0  
860         19.1           92            0.0  
861         19.3           90            0.0  

[845 rows x 9 columns]
             hour     pressure  wind_direction  wind_speed  temperature  \
count  845.000000   845.000000      845.000000  845.000000   845.000000   
mean    10.526627  1004.040473      178.881657    2.323669    27.405799   
std      6.863138     5.459100      112.049510    1.202016     4.896890   
min      0.000000   993.400000        0.000000    0.300000    14.100000   
25%      6.000000  1000.000000       81.000000    1.500000    23.700000   
50%     12.000000  1002.800000      184.000000    2.000000    27.400000   
75%     18.000000  1007.000000      291.000000    2.900000    30.800000   
max     21.000000  1018.400000      360.000000    7.500000    39.400000   

       precipitation  
count     845.000000  
mean        0.132899  
std         0.694294  
min         0.000000  
25%         0.000000  
50%         0.000000  
75%         0.000000  
max         9.400000  

***Repl Closed***


资料:

链接: https://pan.baidu.com/s/1hulCm4RLnn5g4OQz3OkOwQ 密码: hrx7

你可能感兴趣的:(数据分析)