本文实例讲述了Python3.5 Pandas模块缺失值处理和层次索引。分享给大家供大家参考,具体如下:
1、pandas缺失值处理
import numpy as np
import pandas as pd
from pandas import Series,DataFrame
df3 = DataFrame([
["Tom",np.nan,456.67,"M"],
["Merry",34,345.56,np.nan],
[np.nan,np.nan,np.nan,np.nan],
["John",23,np.nan,"M"],
["Joe",18,385.12,"F"]
],columns = ["name","age","salary","gender"])
print(df3)
print("=======判断NaN值=======")
print(df3.isnull())
print("=======判断非NaN值=======")
print(df3.notnull())
print("=======删除包含NaN值的行=======")
print(df3.dropna())
print("=======删除全部为NaN值的行=======")
print(df3.dropna(how="all"))
df3.ix[2,0] = "Gerry" #修改第2行第0列的值
print(df3)
print("=======删除包含NaN值的列=======")
print(df3.dropna(axis=1))
运行结果:
name age salary gender
0 Tom NaN 456.67 M
1 Merry 34.0 345.56 NaN
2 NaN NaN NaN NaN
3 John 23.0 NaN M
4 Joe 18.0 385.12 F
=判断NaN值=
name age salary gender
0 False True False False
1 False False False True
2 True True True True
3 False False True False
4 False False False False
=判断非NaN值=
name age salary gender
0 True False True True
1 True True True False
2 False False False False
3 True True False True
4 True True True True
=删除包含NaN值的行=
name age salary gender
4 Joe 18.0 385.12 F
=删除全部为NaN值的行=
name age salary gender
0 Tom NaN 456.67 M
1 Merry 34.0 345.56 NaN
3 John 23.0 NaN M
4 Joe 18.0 385.12 F
name age salary gender
0 Tom NaN 456.67 M
1 Merry 34.0 345.56 NaN
2 Gerry NaN NaN NaN
3 John 23.