16 2 2019
import pandas as pd
from pandas import DataFrame
import numpy as np

处理丢失数据

  • 有两种丢失数据:

    • None

    • np.nan(NaN)

  • 两种丢失数据的区别

type(None)

 

NoneType

 

type(np.nan)

 

float

 

  • 在pandas中如果遇到了None形式的空值则pandas会将其强转成NAN的形式。

np.nan + 1

 

nan

 

None + 1
---------------------------------------------------------------------------
​
TypeError                                 Traceback (most recent call last)
​
<ipython-input-6-3fd8740bf8ab> in <module>
----> 1 None + 1
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

pandas处理空值操作

  • isnull

  • notnull

  • any

  • all

  • dropna

  • fillna


 
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-12-00cf07b74dcd> in <module>
----> 1 df
NameError: name 'df' is not defined

 

df = pd.read_excel('./data/testData.xlsx')
df

 

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

 

  time none 1 2 3 4 none1 5 6 7
0 2019-01-27 17:00:00 NaN -24.8 -18.2 -20.8 -18.8 NaN NaN NaN NaN
1 2019-01-27 17:01:00 NaN -23.5 -18.8 -20.5 -19.8 NaN -15.2 -14.5 -16.0
2 2019-01-27 17:02:00 NaN -23.2 -19.2 NaN NaN NaN -13.0 NaN -14.0
3 2019-01-27 17:03:00 NaN -22.8 -19.2 -20.0 -20.5 NaN NaN -12.2 -9.8
4 2019-01-27 17:04:00 NaN -23.2 -18.5 -20.0 -18.8 NaN -10.2 -10.8 -8.8
5 2019-01-27 17:05:00 NaN NaN NaN -19.0 -18.2 NaN -10.0 -10.5 -10.8
6 2019-01-27 17:06:00 NaN NaN -18.5 -18.2 -17.5 NaN NaN NaN NaN
7 2019-01-27 17:07:00 NaN -24.8 -18.0 -17.5 -17.2 NaN -14.2 -14.0 -12.5
8 2019-01-27 17:08:00 NaN -25.2 -17.8 NaN NaN NaN -16.2 NaN -14.5
9 2019-01-27 17:09:00 NaN -24.8 -18.2 NaN -17.5 NaN NaN -15.5 -16.0
10 2019-01-27 17:10:00 NaN -24.5 -18.5 -16.0 -18.5 NaN -17.5 -16.5 -17.2
11 2019-01-27 17:11:00 NaN NaN NaN -16.0 -18.5 NaN -17.8 -16.8 -12.0
12 2019-01-27 17:12:00 NaN NaN -18.5 -15.8 -18.8 NaN NaN NaN NaN
13 2019-01-27 17:13:00 NaN -23.8 -18.5 NaN NaN NaN 4.5 NaN 0.0
14 2019-01-27 17:14:00 NaN -23.2 -18.2 NaN -19.0 NaN NaN 5.8 6.8
15 2019-01-27 17:15:00 NaN -23.5 -17.8 -15.0 -18.0 NaN 10.5 10.5 10.8
16 2019-01-27 17:16:00 NaN NaN NaN -14.2 -17.2 NaN 14.0 13.5 13.0
17 2019-01-27 17:17:00 NaN NaN -18.2 -13.8 -17.8 NaN 15.8 15.2 14.2
18 2019-01-27 17:18:00 NaN -23.2 -19.0 -13.8 -18.2 NaN NaN NaN NaN
19 2019-01-27 17:19:00 NaN -23.2 -19.5 NaN NaN NaN 17.8 NaN 15.2
20 2019-01-27 17:20:00 NaN -23.2 -19.8 NaN -19.0 NaN 18.2 17.2 15.8
21 2019-01-27 17:21:00 NaN -23.5 -20.0 -13.8 -19.5 NaN NaN 17.8 16.0
22 2019-01-27 17:22:00 NaN NaN NaN -14.0 -19.5 NaN 18.8 18.0 16.2
23 2019-01-27 17:23:00 NaN -23.2 -20.2 -14.0 -19.5 NaN 19.0 18.2 16.5
24 2019-01-27 17:24:00 NaN NaN -20.2 -14.2 -19.5 NaN NaN NaN NaN
25 2019-01-27 17:25:00 NaN -22.8 -20.5 -14.5 -19.5 NaN 19.2 NaN 16.5
26 2019-01-27 17:26:00 NaN -22.8 -20.8 -15.0 -16.8 NaN NaN 17.2 16.8
27 2019-01-27 17:27:00 NaN -22.0 -16.0 NaN -16.0 NaN 18.8 17.2 16.2
28 2019-01-27 17:28:00 NaN -22.8 -15.2 -14.8 -15.2 NaN 18.8 17.2 16.2
29 2019-01-27 17:29:00 NaN -22.5 -15.0 -14.8 -15.2 NaN 18.8 17.2 16.5
... ... ... ... ... ... ... ... ... ... ...
1030 2019-01-28 10:10:00 NaN -30.5 -27.5 -29.5 -27.8 NaN -3.8 -3.5 -8.2
1031 2019-01-28 10:11:00 NaN -30.8 -27.0 -29.2 -27.8 NaN -3.8 -3.2 -8.5
1032 2019-01-28 10:12:00 NaN -30.5 -26.2 -29.0 -26.8 NaN -3.5 -3.0 -8.8
1033 2019-01-28 10:13:00 NaN -28.8 -25.2 -28.2 -26.2 NaN -3.5 -3.0 -8.8
1034 2019-01-28 10:14:00 NaN -25.2 -25.2 -28.2 -25.8 NaN -3.0 -2.5 -8.8
1035 2019-01-28 10:15:00 NaN -25.2 -25.8 -28.5 -26.2 NaN -3.0 -2.2 -8.5
1036 2019-01-28 10:16:00 NaN -25.8 -26.2 -28.8 -26.8 NaN -2.8 -2.0 -8.2
1037 2019-01-28 10:17:00 NaN -26.2 -26.8 -29.0 -27.2 NaN -2.5 -1.8 -8.2
1038 2019-01-28 10:18:00 NaN -26.5 -27.0 -29.2 -27.5 NaN NaN NaN NaN
1039 2019-01-28 10:19:00 NaN -27.0 -27.2 -29.5 -28.0 NaN -2.2 -1.5 -7.8
1040 2019-01-28 10:20:00 NaN -26.5 -26.8 -29.0 -28.0 NaN -2.2 -1.5 -7.5
1041 2019-01-28 10:21:00 NaN -25.0 -25.8 -28.5 -27.2 NaN -2.2 -1.5 -7.5
1042 2019-01-28 10:22:00 NaN -24.0 -25.2 -28.2 -26.5 NaN -2.0 -1.5 -7.2
1043 2019-01-28 10:23:00 NaN -23.8 -25.0 -28.0 -26.0 NaN -2.0 -1.5 -7.2
1044 2019-01-28 10:24:00 NaN -24.0 -25.2 -28.0 -25.5 NaN -2.0 -1.5 -7.0
1045 2019-01-28 10:25:00 NaN -25.0 -26.0 -28.2 -26.2 NaN -2.2 -1.5 -6.8
1046 2019-01-28 10:26:00 NaN -25.8 -26.5 -28.8 -26.8 NaN -2.2 -1.5 -6.5
1047 2019-01-28 10:27:00 NaN -26.2 -26.5 -28.8 -27.2 NaN -2.2 -1.5 -6.5
1048 2019-01-28 10:28:00 NaN -25.0 -25.8 -28.5 -27.0 NaN -2.2 -1.8 -6.2
1049 2019-01-28 10:29:00 NaN -24.8 -25.2 -28.0 -26.2 NaN -2.2 -1.8 -6.0
1050 2019-01-28 10:30:00 NaN -24.5 -24.8 -27.8 -25.8 NaN -2.0 -2.0 -6.0
1051 2019-01-28 10:31:00 NaN -24.0 -24.8 -27.8 -25.5 NaN -2.0 -2.0 -5.8
1052 2019-01-28 10:32:00 NaN -24.2 -25.5 -28.0 -26.0 NaN -2.0 -2.0 -5.5
1053 2019-01-28 10:33:00 NaN -25.0 -26.2 -28.2 -26.8 NaN -2.0 -2.0 -5.2
1054 2019-01-28 10:34:00 NaN -25.8 -26.8 -28.5 -27.0 NaN -2.0 -2.2 -5.2
1055 2019-01-28 10:35:00 NaN -26.2 -27.2 -28.8 -27.5 NaN -2.0 NaN -5.0
1056 2019-01-28 10:36:00 NaN -26.8 -27.5 -29.0 -27.8 NaN -2.2 NaN -5.0
1057 2019-01-28 10:37:00 NaN -27.2 -27.8 -29.0 -28.0 NaN -2.2 NaN -5.0
1058 2019-01-28 10:38:00 NaN -27.5 -27.0 -29.0 -28.0 NaN -3.5 -3.2 -5.8
1059 2019-01-28 10:39:00 NaN -27.0 -27.2 -29.0 -27.8 NaN -5.0 NaN -7.0

1060 rows × 10 columns

 

df.drop(labels=['none','none1'],axis=1,inplace=True)
#将空值进行清洗
#1.将空对应的行数据删除
#检测哪些行中存在空值
#any用来检测isnull返回的行中是否存在True,如果存在true,则该行返回ture
df.isnull().any(axis=1)

 

0        True
1       False
2        True
3        True
4       False
5        True
6        True
7       False
8        True
9        True
10      False
11       True
12       True
13       True
14       True
15      False
16       True
17       True
18       True
19       True
20       True
21       True
22       True
23      False
24       True
25       True
26       True
27       True
28      False
29      False
        ...  
1030    False
1031    False
1032    False
1033    False
1034    False
1035    False
1036    False
1037    False
1038     True
1039    False
1040    False
1041    False
1042    False
1043    False
1044    False
1045    False
1046    False
1047    False
1048    False
1049    False
1050    False
1051    False
1052    False
1053    False
1054    False
1055     True
1056     True
1057     True
1058    False
1059     True
Length: 1060, dtype: bool

 

df.notnull().all(axis=1) #检测notnull返回的行中是否有False,如果存在false,则该行返回false

 

0       False
1        True
2       False
3       False
4        True
5       False
6       False
7        True
8       False
9       False
10       True
11      False
12      False
13      False
14      False
15       True
16      False
17      False
18      False
19      False
20      False
21      False
22      False
23       True
24      False
25      False
26      False
27      False
28       True
29       True
        ...  
1030     True
1031     True
1032     True
1033     True
1034     True
1035     True
1036     True
1037     True
1038    False
1039     True
1040     True
1041     True
1042     True
1043     True
1044     True
1045     True
1046     True
1047     True
1048     True
1049     True
1050     True
1051     True
1052     True
1053     True
1054     True
1055    False
1056    False
1057    False
1058     True
1059    False
Length: 1060, dtype: bool

 

df.loc[df.notnull().all(axis=1)] #将空对应的行数据进行删除

 

​​

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

 

  time 1 2 3 4 5 6 7
1 2019-01-27 17:01:00 -23.5 -18.8 -20.5 -19.8 -15.2 -14.5 -16.0
4 2019-01-27 17:04:00 -23.2 -18.5 -20.0 -18.8 -10.2 -10.8 -8.8
7 2019-01-27 17:07:00 -24.8 -18.0 -17.5 -17.2 -14.2 -14.0 -12.5
10 2019-01-27 17:10:00 -24.5 -18.5 -16.0 -18.5 -17.5 -16.5 -17.2
15 2019-01-27 17:15:00 -23.5 -17.8 -15.0 -18.0 10.5 10.5 10.8
23 2019-01-27 17:23:00 -23.2 -20.2 -14.0 -19.5 19.0 18.2 16.5
28 2019-01-27 17:28:00 -22.8 -15.2 -14.8 -15.2 18.8 17.2 16.2
29 2019-01-27 17:29:00 -22.5 -15.0 -14.8 -15.2 18.8 17.2 16.5
30 2019-01-27 17:30:00 -22.8 -14.8 -15.0 -15.2 18.8 17.2 16.5
32 2019-01-27 17:32:00 -22.5 -15.0 -14.8 -15.5 18.8 17.2 16.5
33 2019-01-27 17:33:00 -22.5 -15.5 -14.8 -15.5 18.8 17.2 16.5
34 2019-01-27 17:34:00 -22.0 -16.2 -14.5 -15.8 18.8 17.2 16.5
35 2019-01-27 17:35:00 -21.8 -16.8 -14.5 -16.0 18.8 17.2 16.5
36 2019-01-27 17:36:00 -22.0 -17.2 -14.5 -16.5 18.5 17.2 16.5
37 2019-01-27 17:37:00 -22.2 -17.5 -14.5 -17.0 18.5 17.2 16.2
38 2019-01-27 17:38:00 -22.2 -17.8 -14.2 -17.5 18.5 17.2 16.2
39 2019-01-27 17:39:00 -22.8 -18.0 -14.5 -18.0 18.5 17.2 16.2
40 2019-01-27 17:40:00 -22.8 -18.2 -14.2 -18.8 18.2 17.0 16.2
41 2019-01-27 17:41:00 -22.5 -18.5 -14.5 -19.2 18.2 17.0 16.2
42 2019-01-27 17:42:00 -22.2 -18.5 -14.2 -19.8 18.2 17.0 16.0
44 2019-01-27 17:44:00 -23.2 -18.8 -14.5 -19.5 18.0 16.8 16.0
45 2019-01-27 17:45:00 -22.8 -18.8 -14.5 -19.5 18.0 16.8 16.0
46 2019-01-27 17:46:00 -21.8 -18.8 -14.5 -20.0 18.0 16.8 16.0
47 2019-01-27 17:47:00 -22.5 -18.8 -14.5 -20.0 17.8 16.5 15.8
49 2019-01-27 17:49:00 -23.0 -18.5 -14.5 -20.0 17.8 16.5 15.8
50 2019-01-27 17:50:00 -22.0 -18.5 -18.8 -20.0 17.8 16.5 15.8
51 2019-01-27 17:51:00 -20.5 -18.8 -21.2 -20.2 17.5 16.2 15.8
52 2019-01-27 17:52:00 -19.5 -19.5 -22.0 -20.5 17.5 16.5 15.8
53 2019-01-27 17:53:00 -19.0 -19.8 -22.2 -20.5 17.2 16.5 15.8
54 2019-01-27 17:54:00 -18.5 -20.2 -22.5 -20.5 17.2 16.5 16.0
... ... ... ... ... ... ... ... ...
1025 2019-01-28 10:05:00 -31.0 -25.5 -28.5 -26.0 -3.8 -4.2 -5.5
1026 2019-01-28 10:06:00 -31.0 -26.0 -28.5 -25.8 -3.8 -4.0 -6.2
1027 2019-01-28 10:07:00 -30.8 -26.5 -29.0 -26.5 -3.8 -4.0 -6.5
1028 2019-01-28 10:08:00 -30.8 -26.8 -29.2 -27.0 -3.8 -3.8 -7.2
1029 2019-01-28 10:09:00 -30.5 -27.0 -29.2 -27.5 -3.8 -3.8 -7.8
1030 2019-01-28 10:10:00 -30.5 -27.5 -29.5 -27.8 -3.8 -3.5 -8.2
1031 2019-01-28 10:11:00 -30.8 -27.0 -29.2 -27.8 -3.8 -3.2 -8.5
1032 2019-01-28 10:12:00 -30.5 -26.2 -29.0 -26.8 -3.5 -3.0 -8.8
1033 2019-01-28 10:13:00 -28.8 -25.2 -28.2 -26.2 -3.5 -3.0 -8.8
1034 2019-01-28 10:14:00 -25.2 -25.2 -28.2 -25.8 -3.0 -2.5 -8.8
1035 2019-01-28 10:15:00 -25.2 -25.8 -28.5 -26.2 -3.0 -2.2 -8.5
1036 2019-01-28 10:16:00 -25.8 -26.2 -28.8 -26.8 -2.8 -2.0 -8.2
1037 2019-01-28 10:17:00 -26.2 -26.8 -29.0 -27.2 -2.5 -1.8 -8.2
1039 2019-01-28 10:19:00 -27.0 -27.2 -29.5 -28.0 -2.2 -1.5 -7.8
1040 2019-01-28 10:20:00 -26.5 -26.8 -29.0 -28.0 -2.2 -1.5 -7.5
1041 2019-01-28 10:21:00 -25.0 -25.8 -28.5 -27.2 -2.2 -1.5 -7.5
1042 2019-01-28 10:22:00 -24.0 -25.2 -28.2 -26.5 -2.0 -1.5 -7.2
1043 2019-01-28 10:23:00 -23.8 -25.0 -28.0 -26.0 -2.0 -1.5 -7.2
1044 2019-01-28 10:24:00 -24.0 -25.2 -28.0 -25.5 -2.0 -1.5 -7.0
1045 2019-01-28 10:25:00 -25.0 -26.0 -28.2 -26.2 -2.2 -1.5 -6.8
1046 2019-01-28 10:26:00 -25.8 -26.5 -28.8 -26.8 -2.2 -1.5 -6.5
1047 2019-01-28 10:27:00 -26.2 -26.5 -28.8 -27.2 -2.2 -1.5 -6.5
1048 2019-01-28 10:28:00 -25.0 -25.8 -28.5 -27.0 -2.2 -1.8 -6.2
1049 2019-01-28 10:29:00 -24.8 -25.2 -28.0 -26.2 -2.2 -1.8 -6.0
1050 2019-01-28 10:30:00 -24.5 -24.8 -27.8 -25.8 -2.0 -2.0 -6.0
1051 2019-01-28 10:31:00 -24.0 -24.8 -27.8 -25.5 -2.0 -2.0 -5.8
1052 2019-01-28 10:32:00 -24.2 -25.5 -28.0 -26.0 -2.0 -2.0 -5.5
1053 2019-01-28 10:33:00 -25.0 -26.2 -28.2 -26.8 -2.0 -2.0 -5.2
1054 2019-01-28 10:34:00 -25.8 -26.8 -28.5 -27.0 -2.0 -2.2 -5.2
1058 2019-01-28 10:38:00 -27.5 -27.0 -29.0 -28.0 -3.5 -3.2 -5.8

927 rows × 8 columns

#高级用法:直接将存有缺失数据的行删除
df.dropna(axis=0)

 

​​

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

 

  time 1 2 3 4 5 6 7
1 2019-01-27 17:01:00 -23.5 -18.8 -20.5 -19.8 -15.2 -14.5 -16.0
4 2019-01-27 17:04:00 -23.2 -18.5 -20.0 -18.8 -10.2 -10.8 -8.8
7 2019-01-27 17:07:00 -24.8 -18.0 -17.5 -17.2 -14.2 -14.0 -12.5
10 2019-01-27 17:10:00 -24.5 -18.5 -16.0 -18.5 -17.5 -16.5 -17.2
15 2019-01-27 17:15:00 -23.5 -17.8 -15.0 -18.0 10.5 10.5 10.8
23 2019-01-27 17:23:00 -23.2 -20.2 -14.0 -19.5 19.0 18.2 16.5
28 2019-01-27 17:28:00 -22.8 -15.2 -14.8 -15.2 18.8 17.2 16.2
29 2019-01-27 17:29:00 -22.5 -15.0 -14.8 -15.2 18.8 17.2 16.5
30 2019-01-27 17:30:00 -22.8 -14.8 -15.0 -15.2 18.8 17.2 16.5
32 2019-01-27 17:32:00 -22.5 -15.0 -14.8 -15.5 18.8 17.2 16.5
33 2019-01-27 17:33:00 -22.5 -15.5 -14.8 -15.5 18.8 17.2 16.5
34 2019-01-27 17:34:00 -22.0 -16.2 -14.5 -15.8 18.8 17.2 16.5
35 2019-01-27 17:35:00 -21.8 -16.8 -14.5 -16.0 18.8 17.2 16.5
36 2019-01-27 17:36:00 -22.0 -17.2 -14.5 -16.5 18.5 17.2 16.5
37 2019-01-27 17:37:00 -22.2 -17.5 -14.5 -17.0 18.5 17.2 16.2
38 2019-01-27 17:38:00 -22.2 -17.8 -14.2 -17.5 18.5 17.2 16.2
39 2019-01-27 17:39:00 -22.8 -18.0 -14.5 -18.0 18.5 17.2 16.2
40 2019-01-27 17:40:00 -22.8 -18.2 -14.2 -18.8 18.2 17.0 16.2
41 2019-01-27 17:41:00 -22.5 -18.5 -14.5 -19.2 18.2 17.0 16.2
42 2019-01-27 17:42:00 -22.2 -18.5 -14.2 -19.8 18.2 17.0 16.0
44 2019-01-27 17:44:00 -23.2 -18.8 -14.5 -19.5 18.0 16.8 16.0
45 2019-01-27 17:45:00 -22.8 -18.8 -14.5 -19.5 18.0 16.8 16.0
46 2019-01-27 17:46:00 -21.8 -18.8 -14.5 -20.0 18.0 16.8 16.0
47 2019-01-27 17:47:00 -22.5 -18.8 -14.5 -20.0 17.8 16.5 15.8
49 2019-01-27 17:49:00 -23.0 -18.5 -14.5 -20.0 17.8 16.5 15.8
50 2019-01-27 17:50:00 -22.0 -18.5 -18.8 -20.0 17.8 16.5 15.8
51 2019-01-27 17:51:00 -20.5 -18.8 -21.2 -20.2 17.5 16.2 15.8
52 2019-01-27 17:52:00 -19.5 -19.5 -22.0 -20.5 17.5 16.5 15.8
53 2019-01-27 17:53:00 -19.0 -19.8 -22.2 -20.5 17.2 16.5 15.8
54 2019-01-27 17:54:00 -18.5 -20.2 -22.5 -20.5 17.2 16.5 16.0
... ... ... ... ... ... ... ... ...
1025 2019-01-28 10:05:00 -31.0 -25.5 -28.5 -26.0 -3.8 -4.2 -5.5
1026 2019-01-28 10:06:00 -31.0 -26.0 -28.5 -25.8 -3.8 -4.0 -6.2
1027 2019-01-28 10:07:00 -30.8 -26.5 -29.0 -26.5 -3.8 -4.0 -6.5
1028 2019-01-28 10:08:00 -30.8 -26.8 -29.2 -27.0 -3.8 -3.8 -7.2
1029 2019-01-28 10:09:00 -30.5 -27.0 -29.2 -27.5 -3.8 -3.8 -7.8
1030 2019-01-28 10:10:00 -30.5 -27.5 -29.5 -27.8 -3.8 -3.5 -8.2
1031 2019-01-28 10:11:00 -30.8 -27.0 -29.2 -27.8 -3.8 -3.2 -8.5
1032 2019-01-28 10:12:00 -30.5 -26.2 -29.0 -26.8 -3.5 -3.0 -8.8
1033 2019-01-28 10:13:00 -28.8 -25.2 -28.2 -26.2 -3.5 -3.0 -8.8
1034 2019-01-28 10:14:00 -25.2 -25.2 -28.2 -25.8 -3.0 -2.5 -8.8
1035 2019-01-28 10:15:00 -25.2 -25.8 -28.5 -26.2 -3.0 -2.2 -8.5
1036 2019-01-28 10:16:00 -25.8 -26.2 -28.8 -26.8 -2.8 -2.0 -8.2
1037 2019-01-28 10:17:00 -26.2 -26.8 -29.0 -27.2 -2.5 -1.8 -8.2
1039 2019-01-28 10:19:00 -27.0 -27.2 -29.5 -28.0 -2.2 -1.5 -7.8
1040 2019-01-28 10:20:00 -26.5 -26.8 -29.0 -28.0 -2.2 -1.5 -7.5
1041 2019-01-28 10:21:00 -25.0 -25.8 -28.5 -27.2 -2.2 -1.5 -7.5
1042 2019-01-28 10:22:00 -24.0 -25.2 -28.2 -26.5 -2.0 -1.5 -7.2
1043 2019-01-28 10:23:00 -23.8 -25.0 -28.0 -26.0 -2.0 -1.5 -7.2
1044 2019-01-28 10:24:00 -24.0 -25.2 -28.0 -25.5 -2.0 -1.5 -7.0
1045 2019-01-28 10:25:00 -25.0 -26.0 -28.2 -26.2 -2.2 -1.5 -6.8
1046 2019-01-28 10:26:00 -25.8 -26.5 -28.8 -26.8 -2.2 -1.5 -6.5
1047 2019-01-28 10:27:00 -26.2 -26.5 -28.8 -27.2 -2.2 -1.5 -6.5
1048 2019-01-28 10:28:00 -25.0 -25.8 -28.5 -27.0 -2.2 -1.8 -6.2
1049 2019-01-28 10:29:00 -24.8 -25.2 -28.0 -26.2 -2.2 -1.8 -6.0
1050 2019-01-28 10:30:00 -24.5 -24.8 -27.8 -25.8 -2.0 -2.0 -6.0
1051 2019-01-28 10:31:00 -24.0 -24.8 -27.8 -25.5 -2.0 -2.0 -5.8
1052 2019-01-28 10:32:00 -24.2 -25.5 -28.0 -26.0 -2.0 -2.0 -5.5
1053 2019-01-28 10:33:00 -25.0 -26.2 -28.2 -26.8 -2.0 -2.0 -5.2
1054 2019-01-28 10:34:00 -25.8 -26.8 -28.5 -27.0 -2.0 -2.2 -5.2
1058 2019-01-28 10:38:00 -27.5 -27.0 -29.0 -28.0 -3.5 -3.2 -5.8

927 rows × 8 columns

  • 将空值进行填充,使用空值的近邻值进行空值的填充

 

df.fillna(value=-999) #使用指定的值填充所有的空值

 

​​

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

 

  time 1 2 3 4 5 6 7
0 2019-01-27 17:00:00 -24.8 -18.2 -20.8 -18.8 -999.0 -999.0 -999.0
1 2019-01-27 17:01:00 -23.5 -18.8 -20.5 -19.8 -15.2 -14.5 -16.0
2 2019-01-27 17:02:00 -23.2 -19.2 -999.0 -999.0 -13.0 -999.0 -14.0
3 2019-01-27 17:03:00 -22.8 -19.2 -20.0 -20.5 -999.0 -12.2 -9.8
4 2019-01-27 17:04:00 -23.2 -18.5 -20.0 -18.8 -10.2 -10.8 -8.8
5 2019-01-27 17:05:00 -999.0 -999.0 -19.0 -18.2 -10.0 -10.5 -10.8
6 2019-01-27 17:06:00 -999.0 -18.5 -18.2 -17.5 -999.0 -999.0 -999.0
7 2019-01-27 17:07:00 -24.8 -18.0 -17.5 -17.2 -14.2 -14.0 -12.5
8 2019-01-27 17:08:00 -25.2 -17.8 -999.0 -999.0 -16.2 -999.0 -14.5
9 2019-01-27 17:09:00 -24.8 -18.2 -999.0 -17.5 -999.0 -15.5 -16.0
10 2019-01-27 17:10:00 -24.5 -18.5 -16.0 -18.5 -17.5 -16.5 -17.2
11 2019-01-27 17:11:00 -999.0 -999.0 -16.0 -18.5 -17.8 -16.8 -12.0
12 2019-01-27 17:12:00 -999.0 -18.5 -15.8 -18.8 -999.0 -999.0 -999.0
13 2019-01-27 17:13:00 -23.8 -18.5 -999.0 -999.0 4.5 -999.0 0.0
14 2019-01-27 17:14:00 -23.2 -18.2 -999.0 -19.0 -999.0 5.8 6.8
15 2019-01-27 17:15:00 -23.5 -17.8 -15.0 -18.0 10.5 10.5 10.8
16 2019-01-27 17:16:00 -999.0 -999.0 -14.2 -17.2 14.0 13.5 13.0
17 2019-01-27 17:17:00 -999.0 -18.2 -13.8 -17.8 15.8 15.2 14.2
18 2019-01-27 17:18:00 -23.2 -19.0 -13.8 -18.2 -999.0 -999.0 -999.0
19 2019-01-27 17:19:00 -23.2 -19.5 -999.0 -999.0 17.8 -999.0 15.2
20 2019-01-27 17:20:00 -23.2 -19.8 -999.0 -19.0 18.2 17.2 15.8
21 2019-01-27 17:21:00 -23.5 -20.0 -13.8 -19.5 -999.0 17.8 16.0
22 2019-01-27 17:22:00 -999.0 -999.0 -14.0 -19.5 18.8 18.0 16.2
23 2019-01-27 17:23:00 -23.2 -20.2 -14.0 -19.5 19.0 18.2 16.5
24 2019-01-27 17:24:00 -999.0 -20.2 -14.2 -19.5 -999.0 -999.0 -999.0
25 2019-01-27 17:25:00 -22.8 -20.5 -14.5 -19.5 19.2 -999.0 16.5
26 2019-01-27 17:26:00 -22.8 -20.8 -15.0 -16.8 -999.0 17.2 16.8
27 2019-01-27 17:27:00 -22.0 -16.0 -999.0 -16.0 18.8 17.2 16.2
28 2019-01-27 17:28:00 -22.8 -15.2 -14.8 -15.2 18.8 17.2 16.2
29 2019-01-27 17:29:00 -22.5 -15.0 -14.8 -15.2 18.8 17.2 16.5
... ... ... ... ... ... ... ... ...
1030 2019-01-28 10:10:00 -30.5 -27.5 -29.5 -27.8 -3.8 -3.5 -8.2
1031 2019-01-28 10:11:00 -30.8 -27.0 -29.2 -27.8 -3.8 -3.2 -8.5
1032 2019-01-28 10:12:00 -30.5 -26.2 -29.0 -26.8 -3.5 -3.0 -8.8
1033 2019-01-28 10:13:00 -28.8 -25.2 -28.2 -26.2 -3.5 -3.0 -8.8
1034 2019-01-28 10:14:00 -25.2 -25.2 -28.2 -25.8 -3.0 -2.5 -8.8
1035 2019-01-28 10:15:00 -25.2 -25.8 -28.5 -26.2 -3.0 -2.2 -8.5
1036 2019-01-28 10:16:00 -25.8 -26.2 -28.8 -26.8 -2.8 -2.0 -8.2
1037 2019-01-28 10:17:00 -26.2 -26.8 -29.0 -27.2 -2.5 -1.8 -8.2
1038 2019-01-28 10:18:00 -26.5 -27.0 -29.2 -27.5 -999.0 -999.0 -999.0
1039 2019-01-28 10:19:00 -27.0 -27.2 -29.5 -28.0 -2.2 -1.5 -7.8
1040 2019-01-28 10:20:00 -26.5 -26.8 -29.0 -28.0 -2.2 -1.5 -7.5
1041 2019-01-28 10:21:00 -25.0 -25.8 -28.5 -27.2 -2.2 -1.5 -7.5
1042 2019-01-28 10:22:00 -24.0 -25.2 -28.2 -26.5 -2.0 -1.5 -7.2
1043 2019-01-28 10:23:00 -23.8 -25.0 -28.0 -26.0 -2.0 -1.5 -7.2
1044 2019-01-28 10:24:00 -24.0 -25.2 -28.0 -25.5 -2.0 -1.5 -7.0
1045 2019-01-28 10:25:00 -25.0 -26.0 -28.2 -26.2 -2.2 -1.5 -6.8
1046 2019-01-28 10:26:00 -25.8 -26.5 -28.8 -26.8 -2.2 -1.5 -6.5
1047 2019-01-28 10:27:00 -26.2 -26.5 -28.8 -27.2 -2.2 -1.5 -6.5
1048 2019-01-28 10:28:00 -25.0 -25.8 -28.5 -27.0 -2.2 -1.8 -6.2
1049 2019-01-28 10:29:00 -24.8 -25.2 -28.0 -26.2 -2.2 -1.8 -6.0
1050 2019-01-28 10:30:00 -24.5 -24.8 -27.8 -25.8 -2.0 -2.0 -6.0
1051 2019-01-28 10:31:00 -24.0 -24.8 -27.8 -25.5 -2.0 -2.0 -5.8
1052 2019-01-28 10:32:00 -24.2 -25.5 -28.0 -26.0 -2.0 -2.0 -5.5
1053 2019-01-28 10:33:00 -25.0 -26.2 -28.2 -26.8 -2.0 -2.0 -5.2
1054 2019-01-28 10:34:00 -25.8 -26.8 -28.5 -27.0 -2.0 -2.2 -5.2
1055 2019-01-28 10:35:00 -26.2 -27.2 -28.8 -27.5 -2.0 -999.0 -5.0
1056 2019-01-28 10:36:00 -26.8 -27.5 -29.0 -27.8 -2.2 -999.0 -5.0
1057 2019-01-28 10:37:00 -27.2 -27.8 -29.0 -28.0 -2.2 -999.0 -5.0
1058 2019-01-28 10:38:00 -27.5 -27.0 -29.0 -28.0 -3.5 -3.2 -5.8
1059 2019-01-28 10:39:00 -27.0 -27.2 -29.0 -27.8 -5.0 -999.0 -7.0

1060 rows × 8 columns

 

 

df.fillna(method='bfill',axis=0) #使用近邻值填充

 

​​

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

 

  time 1 2 3 4 5 6 7
0 2019-01-27 17:00:00 -24.8 -18.2 -20.8 -18.8 -15.2 -14.5 -16.0
1 2019-01-27 17:01:00 -23.5 -18.8 -20.5 -19.8 -15.2 -14.5 -16.0
2 2019-01-27 17:02:00 -23.2 -19.2 -20.0 -20.5 -13.0 -12.2 -14.0
3 2019-01-27 17:03:00 -22.8 -19.2 -20.0 -20.5 -10.2 -12.2 -9.8
4 2019-01-27 17:04:00 -23.2 -18.5 -20.0 -18.8 -10.2 -10.8 -8.8
5 2019-01-27 17:05:00 -24.8 -18.5 -19.0 -18.2 -10.0 -10.5 -10.8
6 2019-01-27 17:06:00 -24.8 -18.5 -18.2 -17.5 -14.2 -14.0 -12.5
7 2019-01-27 17:07:00 -24.8 -18.0 -17.5 -17.2 -14.2 -14.0 -12.5
8 2019-01-27 17:08:00 -25.2 -17.8 -16.0 -17.5 -16.2 -15.5 -14.5
9 2019-01-27 17:09:00 -24.8 -18.2 -16.0 -17.5 -17.5 -15.5 -16.0
10 2019-01-27 17:10:00 -24.5 -18.5 -16.0 -18.5 -17.5 -16.5 -17.2
11 2019-01-27 17:11:00 -23.8 -18.5 -16.0 -18.5 -17.8 -16.8 -12.0
12 2019-01-27 17:12:00 -23.8 -18.5 -15.8 -18.8 4.5 5.8 0.0
13 2019-01-27 17:13:00 -23.8 -18.5 -15.0 -19.0 4.5 5.8 0.0
14 2019-01-27 17:14:00 -23.2 -18.2 -15.0 -19.0 10.5 5.8 6.8
15 2019-01-27 17:15:00 -23.5 -17.8 -15.0 -18.0 10.5 10.5 10.8
16 2019-01-27 17:16:00 -23.2 -18.2 -14.2 -17.2 14.0 13.5 13.0
17 2019-01-27 17:17:00 -23.2 -18.2 -13.8 -17.8 15.8 15.2 14.2
18 2019-01-27 17:18:00 -23.2 -19.0 -13.8 -18.2 17.8 17.2 15.2
19 2019-01-27 17:19:00 -23.2 -19.5 -13.8 -19.0 17.8 17.2 15.2
20 2019-01-27 17:20:00 -23.2 -19.8 -13.8 -19.0 18.2 17.2 15.8
21 2019-01-27 17:21:00 -23.5 -20.0 -13.8 -19.5 18.8 17.8 16.0
22 2019-01-27 17:22:00 -23.2 -20.2 -14.0 -19.5 18.8 18.0 16.2
23 2019-01-27 17:23:00 -23.2 -20.2 -14.0 -19.5 19.0 18.2 16.5
24 2019-01-27 17:24:00 -22.8 -20.2 -14.2 -19.5 19.2 17.2 16.5
25 2019-01-27 17:25:00 -22.8 -20.5 -14.5 -19.5 19.2 17.2 16.5
26 2019-01-27 17:26:00 -22.8 -20.8 -15.0 -16.8 18.8 17.2 16.8
27 2019-01-27 17:27:00 -22.0 -16.0 -14.8 -16.0 18.8 17.2 16.2
28 2019-01-27 17:28:00 -22.8 -15.2 -14.8 -15.2 18.8 17.2 16.2
29 2019-01-27 17:29:00 -22.5 -15.0 -14.8 -15.2 18.8 17.2 16.5
... ... ... ... ... ... ... ... ...
1030 2019-01-28 10:10:00 -30.5 -27.5 -29.5 -27.8 -3.8 -3.5 -8.2
1031 2019-01-28 10:11:00 -30.8 -27.0 -29.2 -27.8 -3.8 -3.2 -8.5
1032 2019-01-28 10:12:00 -30.5 -26.2 -29.0 -26.8 -3.5 -3.0 -8.8
1033 2019-01-28 10:13:00 -28.8 -25.2 -28.2 -26.2 -3.5 -3.0 -8.8
1034 2019-01-28 10:14:00 -25.2 -25.2 -28.2 -25.8 -3.0 -2.5 -8.8
1035 2019-01-28 10:15:00 -25.2 -25.8 -28.5 -26.2 -3.0 -2.2 -8.5
1036 2019-01-28 10:16:00 -25.8 -26.2 -28.8 -26.8 -2.8 -2.0 -8.2
1037 2019-01-28 10:17:00 -26.2 -26.8 -29.0 -27.2 -2.5 -1.8 -8.2
1038 2019-01-28 10:18:00 -26.5 -27.0 -29.2 -27.5 -2.2 -1.5 -7.8
1039 2019-01-28 10:19:00 -27.0 -27.2 -29.5 -28.0 -2.2 -1.5 -7.8
1040 2019-01-28 10:20:00 -26.5 -26.8 -29.0 -28.0 -2.2 -1.5 -7.5
1041 2019-01-28 10:21:00 -25.0 -25.8 -28.5 -27.2 -2.2 -1.5 -7.5
1042 2019-01-28 10:22:00 -24.0 -25.2 -28.2 -26.5 -2.0 -1.5 -7.2
1043 2019-01-28 10:23:00 -23.8 -25.0 -28.0 -26.0 -2.0 -1.5 -7.2
1044 2019-01-28 10:24:00 -24.0 -25.2 -28.0 -25.5 -2.0 -1.5 -7.0
1045 2019-01-28 10:25:00 -25.0 -26.0 -28.2 -26.2 -2.2 -1.5 -6.8
1046 2019-01-28 10:26:00 -25.8 -26.5 -28.8 -26.8 -2.2 -1.5 -6.5
1047 2019-01-28 10:27:00 -26.2 -26.5 -28.8 -27.2 -2.2 -1.5 -6.5
1048 2019-01-28 10:28:00 -25.0 -25.8 -28.5 -27.0 -2.2 -1.8 -6.2
1049 2019-01-28 10:29:00 -24.8 -25.2 -28.0 -26.2 -2.2 -1.8 -6.0
1050 2019-01-28 10:30:00 -24.5 -24.8 -27.8 -25.8 -2.0 -2.0 -6.0
1051 2019-01-28 10:31:00 -24.0 -24.8 -27.8 -25.5 -2.0 -2.0 -5.8
1052 2019-01-28 10:32:00 -24.2 -25.5 -28.0 -26.0 -2.0 -2.0 -5.5
1053 2019-01-28 10:33:00 -25.0 -26.2 -28.2 -26.8 -2.0 -2.0 -5.2
1054 2019-01-28 10:34:00 -25.8 -26.8 -28.5 -27.0 -2.0 -2.2 -5.2
1055 2019-01-28 10:35:00 -26.2 -27.2 -28.8 -27.5 -2.0 -3.2 -5.0
1056 2019-01-28 10:36:00 -26.8 -27.5 -29.0 -27.8 -2.2 -3.2 -5.0
1057 2019-01-28 10:37:00 -27.2 -27.8 -29.0 -28.0 -2.2 -3.2 -5.0
1058 2019-01-28 10:38:00 -27.5 -27.0 -29.0 -28.0 -3.5 -3.2 -5.8
1059 2019-01-28 10:39:00 -27.0 -27.2 -29.0 -27.8 -5.0 NaN -7.0

1060 rows × 8 columns

 

 

df

 

​​

.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}

 

  time 1 2 3 4 5 6 7
0 2019-01-27 17:00:00 -24.8 -18.2 -20.8 -18.8 NaN NaN NaN
1 2019-01-27 17:01:00 -23.5 -18.8 -20.5 -19.8 -15.2 -14.5 -16.0
2 2019-01-27 17:02:00 -23.2 -19.2 NaN NaN -13.0 NaN -14.0
3 2019-01-27 17:03:00 -22.8 -19.2 -20.0 -20.5 NaN -12.2 -9.8
4 2019-01-27 17:04:00 -23.2 -18.5 -20.0 -18.8 -10.2 -10.8 -8.8
5 2019-01-27 17:05:00 NaN NaN -19.0 -18.2 -10.0 -10.5 -10.8
6 2019-01-27 17:06:00 NaN -18.5 -18.2 -17.5 NaN NaN NaN
7 2019-01-27 17:07:00 -24.8 -18.0 -17.5 -17.2 -14.2 -14.0 -12.5
8 2019-01-27 17:08:00 -25.2 -17.8 NaN NaN -16.2 NaN -14.5
9 2019-01-27 17:09:00 -24.8 -18.2 NaN -17.5 NaN -15.5 -16.0
10 2019-01-27 17:10:00 -24.5 -18.5 -16.0 -18.5 -17.5 -16.5 -17.2
11 2019-01-27 17:11:00 NaN NaN -16.0 -18.5 -17.8 -16.8 -12.0
12 2019-01-27 17:12:00 NaN -18.5 -15.8 -18.8 NaN NaN NaN
13 2019-01-27 17:13:00 -23.8 -18.5 NaN NaN 4.5 NaN 0.0
14 2019-01-27 17:14:00 -23.2 -18.2 NaN -19.0 NaN 5.8 6.8
15 2019-01-27 17:15:00 -23.5 -17.8 -15.0 -18.0 10.5 10.5 10.8
16 2019-01-27 17:16:00 NaN NaN -14.2 -17.2 14.0 13.5 13.0
17 2019-01-27 17:17:00 NaN -18.2 -13.8 -17.8 15.8 15.2 14.2
18 2019-01-27 17:18:00 -23.2 -19.0 -13.8 -18.2 NaN NaN NaN
19 2019-01-27 17:19:00 -23.2 -19.5 NaN NaN 17.8 NaN 15.2
20 2019-01-27 17:20:00 -23.2 -19.8 NaN -19.0 18.2 17.2 15.8
21 2019-01-27 17:21:00 -23.5 -20.0 -13.8 -19.5 NaN 17.8 16.0
22 2019-01-27 17:22:00 NaN NaN -14.0 -19.5 18.8 18.0 16.2
23 2019-01-27 17:23:00 -23.2 -20.2 -14.0 -19.5 19.0 18.2 16.5
24 2019-01-27 17:24:00 NaN -20.2 -14.2 -19.5 NaN NaN NaN
25 2019-01-27 17:25:00 -22.8 -20.5 -14.5 -19.5 19.2 NaN 16.5
26 2019-01-27 17:26:00 -22.8 -20.8 -15.0 -16.8 NaN 17.2 16.8
27 2019-01-27 17:27:00 -22.0 -16.0 NaN -16.0 18.8 17.2 16.2
28 2019-01-27 17:28:00 -22.8 -15.2 -14.8 -15.2 18.8 17.2 16.2
29 2019-01-27 17:29:00 -22.5 -15.0 -14.8 -15.2 18.8 17.2 16.5
... ... ... ... ... ... ... ... ...
1030 2019-01-28 10:10:00 -30.5 -27.5 -29.5 -27.8 -3.8 -3.5 -8.2
1031 2019-01-28 10:11:00 -30.8 -27.0 -29.2 -27.8 -3.8 -3.2 -8.5
1032 2019-01-28 10:12:00 -30.5 -26.2 -29.0 -26.8 -3.5 -3.0 -8.8
1033 2019-01-28 10:13:00 -28.8 -25.2 -28.2 -26.2 -3.5 -3.0 -8.8
1034 2019-01-28 10:14:00 -25.2 -25.2 -28.2 -25.8 -3.0 -2.5 -8.8
1035 2019-01-28 10:15:00 -25.2 -25.8 -28.5 -26.2 -3.0 -2.2 -8.5
1036 2019-01-28 10:16:00 -25.8 -26.2 -28.8 -26.8 -2.8 -2.0 -8.2
1037 2019-01-28 10:17:00 -26.2 -26.8 -29.0 -27.2 -2.5 -1.8 -8.2
1038 2019-01-28 10:18:00 -26.5 -27.0 -29.2 -27.5 NaN NaN NaN
1039 2019-01-28 10:19:00 -27.0 -27.2 -29.5 -28.0 -2.2 -1.5 -7.8
1040 2019-01-28 10:20:00 -26.5 -26.8 -29.0 -28.0 -2.2 -1.5 -7.5
1041 2019-01-28 10:21:00 -25.0 -25.8 -28.5 -27.2 -2.2 -1.5 -7.5
1042 2019-01-28 10:22:00 -24.0 -25.2 -28.2 -26.5 -2.0 -1.5 -7.2
1043 2019-01-28 10:23:00 -23.8 -25.0 -28.0 -26.0 -2.0 -1.5 -7.2
1044 2019-01-28 10:24:00 -24.0 -25.2 -28.0 -25.5 -2.0 -1.5 -7.0
1045 2019-01-28 10:25:00 -25.0 -26.0 -28.2 -26.2 -2.2 -1.5 -6.8
1046 2019-01-28 10:26:00 -25.8 -26.5 -28.8 -26.8 -2.2 -1.5 -6.5
1047 2019-01-28 10:27:00 -26.2 -26.5 -28.8 -27.2 -2.2 -1.5 -6.5
1048 2019-01-28 10:28:00 -25.0 -25.8 -28.5 -27.0 -2.2 -1.8 -6.2
1049 2019-01-28 10:29:00 -24.8 -25.2 -28.0 -26.2 -2.2 -1.8 -6.0
1050 2019-01-28 10:30:00 -24.5 -24.8 -27.8 -25.8 -2.0 -2.0 -6.0
1051 2019-01-28 10:31:00 -24.0 -24.8 -27.8 -25.5 -2.0 -2.0 -5.8
1052 2019-01-28 10:32:00 -24.2 -25.5 -28.0 -26.0 -2.0 -2.0 -5.5
1053 2019-01-28 10:33:00 -25.0 -26.2 -28.2 -26.8 -2.0 -2.0 -5.2
1054 2019-01-28 10:34:00 -25.8 -26.8 -28.5 -27.0 -2.0 -2.2 -5.2
1055 2019-01-28 10:35:00 -26.2 -27.2 -28.8 -27.5 -2.0 NaN -5.0
1056 2019-01-28 10:36:00 -26.8 -27.5 -29.0 -27.8 -2.2 NaN -5.0
1057 2019-01-28 10:37:00 -27.2 -27.8 -29.0 -28.0 -2.2 NaN -5.0
1058 2019-01-28 10:38:00 -27.5 -27.0 -29.0 -28.0 -3.5 -3.2 -5.8
1059 2019-01-28 10:39:00 -27.0 -27.2 -29.0 -27.8 -5.0 NaN -7.0

1060 rows × 8 columns

 

 

 

 

延伸阅读
    < /body> < /html>