pandas_fillna&dropna

數(shù)據(jù)源:鏈接: https://pan.baidu.com/s/1EFqJFXf70t2Rubkh6D19aw 提取碼: syqg
數(shù)據(jù)源示例:

探索Iris紙鳶花數(shù)據(jù)

步驟1 導(dǎo)入必要的庫

import pandas as pd
import numpy as np

步驟2 數(shù)據(jù)集地址

path1='pandas_exercise\exercise_data\iris.csv'

步驟3 將數(shù)據(jù)集存成變量iris

iris=pd.read_csv(path1)
print(iris.head())

步驟4 創(chuàng)建數(shù)據(jù)框的列名稱

iris=pd.read_csv(path1,names = ['sepal_length','sepal_width', 'petal_length', 'petal_width', 'class'])
print(iris.head())

步驟5 數(shù)據(jù)框中有缺失值嗎?

print(pd.isnull(iris).sum())

步驟6 將列petal_length的第10到19行設(shè)置為缺失值

iris.iloc[10:20,2:3]=np.nan
print(iris.head(20))

步驟7 將缺失值全部替換為1.0

iris.fillna(1,inplace=True)
print(iris.head(20))

步驟8 刪除列class

del iris['class']

步驟9 將數(shù)據(jù)框前三行設(shè)置為缺失值

iris.iloc[:3,:]=np.nan
print(iris.head())

步驟10 刪除有缺失值的行

iris=iris.dropna(how='any')

步驟11 重新設(shè)置索引

iris=iris.reset_index(drop=True)
print(iris.head())

# 步驟3
   5.1  3.5  1.4  0.2  Iris-setosa
0  4.9  3.0  1.4  0.2  Iris-setosa
1  4.7  3.2  1.3  0.2  Iris-setosa
2  4.6  3.1  1.5  0.2  Iris-setosa
3  5.0  3.6  1.4  0.2  Iris-setosa
4  5.4  3.9  1.7  0.4  Iris-setosa
# 步驟4
   sepal_length  sepal_width  petal_length  petal_width        class
0           5.1          3.5           1.4          0.2  Iris-setosa
1           4.9          3.0           1.4          0.2  Iris-setosa
2           4.7          3.2           1.3          0.2  Iris-setosa
3           4.6          3.1           1.5          0.2  Iris-setosa
4           5.0          3.6           1.4          0.2  Iris-setosa
# 步驟5
sepal_length    0
sepal_width     0
petal_length    0
petal_width     0
class           0
dtype: int64
# 步驟6
    sepal_length  sepal_width  petal_length  petal_width        class
0            5.1          3.5           1.4          0.2  Iris-setosa
1            4.9          3.0           1.4          0.2  Iris-setosa
2            4.7          3.2           1.3          0.2  Iris-setosa
3            4.6          3.1           1.5          0.2  Iris-setosa
4            5.0          3.6           1.4          0.2  Iris-setosa
5            5.4          3.9           1.7          0.4  Iris-setosa
6            4.6          3.4           1.4          0.3  Iris-setosa
7            5.0          3.4           1.5          0.2  Iris-setosa
8            4.4          2.9           1.4          0.2  Iris-setosa
9            4.9          3.1           1.5          0.1  Iris-setosa
10           5.4          3.7           NaN          0.2  Iris-setosa
11           4.8          3.4           NaN          0.2  Iris-setosa
12           4.8          3.0           NaN          0.1  Iris-setosa
13           4.3          3.0           NaN          0.1  Iris-setosa
14           5.8          4.0           NaN          0.2  Iris-setosa
15           5.7          4.4           NaN          0.4  Iris-setosa
16           5.4          3.9           NaN          0.4  Iris-setosa
17           5.1          3.5           NaN          0.3  Iris-setosa
18           5.7          3.8           NaN          0.3  Iris-setosa
19           5.1          3.8           NaN          0.3  Iris-setosa
# 步驟7
    sepal_length  sepal_width  petal_length  petal_width        class
0            5.1          3.5           1.4          0.2  Iris-setosa
1            4.9          3.0           1.4          0.2  Iris-setosa
2            4.7          3.2           1.3          0.2  Iris-setosa
3            4.6          3.1           1.5          0.2  Iris-setosa
4            5.0          3.6           1.4          0.2  Iris-setosa
5            5.4          3.9           1.7          0.4  Iris-setosa
6            4.6          3.4           1.4          0.3  Iris-setosa
7            5.0          3.4           1.5          0.2  Iris-setosa
8            4.4          2.9           1.4          0.2  Iris-setosa
9            4.9          3.1           1.5          0.1  Iris-setosa
10           5.4          3.7           1.0          0.2  Iris-setosa
11           4.8          3.4           1.0          0.2  Iris-setosa
12           4.8          3.0           1.0          0.1  Iris-setosa
13           4.3          3.0           1.0          0.1  Iris-setosa
14           5.8          4.0           1.0          0.2  Iris-setosa
15           5.7          4.4           1.0          0.4  Iris-setosa
16           5.4          3.9           1.0          0.4  Iris-setosa
17           5.1          3.5           1.0          0.3  Iris-setosa
18           5.7          3.8           1.0          0.3  Iris-setosa
19           5.1          3.8           1.0          0.3  Iris-setosa
# 步驟9
   sepal_length  sepal_width  petal_length  petal_width
0           NaN          NaN           NaN          NaN
1           NaN          NaN           NaN          NaN
2           NaN          NaN           NaN          NaN
3           4.6          3.1           1.5          0.2
4           5.0          3.6           1.4          0.2
# 步驟11
   sepal_length  sepal_width  petal_length  petal_width
0           4.6          3.1           1.5          0.2
1           5.0          3.6           1.4          0.2
2           5.4          3.9           1.7          0.4
3           4.6          3.4           1.4          0.3
4           5.0          3.4           1.5          0.2

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容