1.创建有空值的DataFrame
import numpy as np
import pandas as pd
dates = pd.date_range("20200307", periods=4)
df1 = pd.DataFrame(np.arange(12).reshape(4, 3), index=dates, columns=["A", "B", "C"])
df2 = pd.DataFrame(df1, index=dates, columns=["A", "B", "C", "D"]) # 新增D列,却不赋值,NaN表示空值
print(df2)
# 打印输出:
# A B C D
# 2020-03-07 0 1 2 NaN
# 2020-03-08 3 4 5 NaN
# 2020-03-09 6 7 8 NaN
# 2020-03-10 9 10 11 NaN
2.检查是否有空值
print(df2.isnull()) # 是空值返回True,否则返回False
print(np.any(df2.isnull())) # 只要有一个空值便会返回True,否则返回False
print(np.all(df2.isnull())) # 全部值都是空值便会返回True,否则返回False
# 输出结果:
# A B C D
# 2020-03-07 False False False True
# 2020-03-08 False False False True
# 2020-03-09 False False False True
# 2020-03-10 False False False True
# True
# False
3.给NaN赋值
df2.iloc[0, 3] = 10 # 直接给某个位置赋值
print(df2)
# 打印输出:
# A B C D
# 2020-03-07 0 1 2 10.0
# 2020-03-08 3 4 5 NaN
# 2020-03-09 6 7 8 NaN
# 2020-03-10 9 10 11 NaN
series = pd.Series([11, 12, 13], index=dates[1:4])
df2["D"] = series # 同时给D列赋多个值
print(df2)
# 打印输出:
# A B C D
# 2020-03-07 0 1 2 NaN
# 2020-03-08 3 4 5 11.0
# 2020-03-09 6 7 8 12.0
# 2020-03-10 9 10 11 13.0
4.去除有空值的行或列
df2.loc["2020-03-10", ["A", "B", "C"]] = [11, 12, 15]
df2.fillna("null") # 把空值填充成null
# dropna(axis,how,subset)方法会删除有空值的行或列,
# axis为0是行,axis为1是列,
# how为any时该行或列只要有一个空值就会删除,all是全都是空值才删除
# subset是一个列表,指定某些列
df2.dropna(axis=0, how="any", subset=["A", "D"])