Pandas 删除含有空值的行或列
import pandas as pd
import numpy as np
df1 = pd.DataFrame(
[
[np.nan, 2, np.nan, 0],
[3, 4, np.nan, 1],
[np.nan, np.nan, np.nan, 5],
[np.nan, 3, np.nan, 4]
],columns=list(‘ABCD’))
print(df1)
df2=df1.copy()
df1[‘A’]=df1[‘A’].fillna(‘null’) #将 df 中 A 列所有空值赋值为’null’
print(df1)
df1=df1[~df1[‘A’].isin([‘null’])]
print(df1)
#删除某行空值所在列
df2[0:1]=df2[0:1].fillna(‘null’)
print(df2)
cols=[x for i,x in enumerate(df2.columns) if df2.iat[0,i]==‘null’]
print(cols)
df2=df2.drop(cols,axis=1)
print(df2)
666
Pandas 删除含有空值的行:
df.dropna(axis=0)
列:
df.dropna(axis=1)