pandas——如何处理NaN数据

函数 含义
dropna(axis, how) 删除整行或整列(axis是按照啥方向, how是删的条件,这俩都是可选的)
d.fillna(num) 用num来替代其中的NaN
d.isnull() 返回一个bool matrix,NaN表示为True
np.any(d.isnull()) == True 判断d中有没有NaN

axis挺迷的:
知乎的

dropna(self, axis, how)

import pandas as pd
import numpy as np

d = pd.DataFrame(np.linspace(2, 17, 16).reshape(4, 4), ['a', 'b', 'c', 'd'], ['A', 2, 3, 4])
d.loc['b', 3] = np.nan
d.loc['c', 2] = np.nan
print(type(d))  # 
print(d)
print(d.dropna())  #

pandas——如何处理NaN数据_第1张图片

print(d.dropna(axis=1))

pandas——如何处理NaN数据_第2张图片
how:

  • any:表示只要有NaN就删
  • all:表示全都是NaN才删
print(d.dropna(how='all'))

pandas——如何处理NaN数据_第3张图片

d.fillna(num)

import pandas as pd
import numpy as np

d = pd.DataFrame(np.linspace(2, 17, 16).reshape(4, 4), ['a', 'b', 'c', 'd'], ['A', 2, 3, 4])
d.loc['b', 3] = np.nan
d.loc['c', 2] = np.nan
print(type(d))  # 
print(d)
print(d.fillna(-1))

pandas——如何处理NaN数据_第4张图片

d.isnull()

import pandas as pd
import numpy as np

d = pd.DataFrame(np.linspace(2, 17, 16).reshape(4, 4), ['a', 'b', 'c', 'd'], ['A', 2, 3, 4])
d.loc['b', 3] = np.nan
d.loc['c', 2] = np.nan
print(type(d))  # 
print(d)
print(d.isnull())

pandas——如何处理NaN数据_第5张图片

np.any(d.isnull()) == True

import pandas as pd
import numpy as np

d = pd.DataFrame(np.linspace(2, 17, 16).reshape(4, 4), ['a', 'b', 'c', 'd'], ['A', 2, 3, 4])
d.loc['b', 3] = np.nan
d.loc['c', 2] = np.nan
print(type(d))  # 
print(d)
print(np.any(d.isnull()) == True)

你可能感兴趣的:(python)