官术网_书友最值得收藏!

Working with missing data

In this section, we will discuss missing, NaN, or null values, in Pandas data structures. It is a very common situation to arrive with missing data in an object. One such case that creates missing data is reindexing:

>>> df8 = pd.DataFrame(np.arange(12).reshape(4,3), 
 columns=['a', 'b', 'c'])
 a b c
0 0 1 2
1 3 4 5
2 6 7 8
3 9 10 11
>>> df9 = df8.reindex(columns = ['a', 'b', 'c', 'd'])
 a b c d
0 0 1 2 NaN
1 3 4 5 NaN
2 6 7 8 NaN
4 9 10 11 NaN
>>> df10 = df8.reindex([3, 2, 'a', 0])
 a b c
3 9 10 11
2 6 7 8
a NaN NaN NaN
0 0 1 2

To manipulate missing values, we can use the isnull() or notnull() functions to detect the missing values in a Series object, as well as in a DataFrame object:

>>> df10.isnull()
 a b c
3 False False False
2 False False False
a True True True
0 False False False

On a Series, we can drop all null data and index values by using the dropna function:

>>> s4 = pd.Series({'001': 'Nam', '002': 'Mary',
 '003': 'Peter'},
 index=['002', '001', '024', '065'])
>>> s4
002 Mary
001 Nam
024 NaN
065 NaN
dtype: object
>>> s4.dropna() # dropping all null value of Series object
002 Mary
001 Nam
dtype: object

With a DataFrame object, it is a little bit more complex than with Series. We can tell which rows or columns we want to drop and also if all entries must be null or a single null value is enough. By default, the function will drop any row containing a missing value:

>>> df9.dropna() # all rows will be dropped
Empty DataFrame
Columns: [a, b, c, d]
Index: []
>>> df9.dropna(axis=1)
 a b c
0 0 1 2
1 3 4 5
2 6 7 8
3 9 10 11

Another way to control missing values is to use the supported parameters of functions that we introduced in the previous section. They are also very useful to solve this problem. In our experience, we should assign a fixed value in missing cases when we create data objects. This will make our objects cleaner in later processing steps. For example, consider the following:

>>> df11 = df8.reindex([3, 2, 'a', 0], fill_value = 0)
>>> df11
 a b c
3 9 10 11
2 6 7 8
a 0 0 0
0 0 1 2

We can alse use the fillna function to fill a custom value in missing values:

>>> df9.fillna(-1)
 a b c d
0 0 1 2 -1
1 3 4 5 -1
2 6 7 8 -1
3 9 10 11 -1
主站蜘蛛池模板: 焉耆| 清流县| 昔阳县| 化德县| 张家港市| 米易县| 漠河县| 阿克苏市| 那坡县| 确山县| 隆林| 阳春市| 昭苏县| 双鸭山市| 洛扎县| 漠河县| 连州市| 东乌| 沙雅县| 舒兰市| 新干县| 民勤县| 星子县| 红原县| 永寿县| 吴堡县| 类乌齐县| 承德市| 阜平县| 克什克腾旗| 耒阳市| 新丰县| 南阳市| 扎赉特旗| 应用必备| 灵寿县| 宝鸡市| 德保县| 东宁县| 革吉县| 三明市|