import pandas as pd
import numpy as np
df=pd.DataFrame({"record":[np.nan,"亚健康|潘光|45岁","疾病|张思",np.nan],"date":[np.nan,20210102,20210103,20210104]},index=["one","two","three","four"])
df.dropna()#默认axis=0
df.dropna(axis=1)
df.dropna(axis=0,how="all")
df.dropna(subset=["record"],axis=0)
以上如果需要在原数据上直接做更改,需设置参数inplace=True
df=pd.DataFrame({'state':[1,1,2,2,1,2,2],'pop':['a','b','c','d','b','c','d']})
语法:drop_duplicates(subset,keep,inplace),其中参数 keep:{‘first’,‘last’,False},默认’first’
first
:保留第一次出现的重复项,删除第二次及之后出现的重复项。
last
:保留最后一次出现的重复项,删除之前出现的重复项。
"false"
:删除所有重复项。
1)keep=“first”
df.drop_duplicates(keep="first")
2)keep=“last”
df.drop_duplicates(keep="last")
3)keep=False
df.drop_duplicates(keep=False)
df.drop_duplicates(subset=["state"],keep="first")
以上如果需要在原数据上直接做更改,需设置参数inplace=True
df=pd.DataFrame(np.arange(16).reshape(4,4),columns=["one","two","three","four"])
df.drop(["one"],axis=1)
另外,也可通过del df["one"]
来实现删除指定列,但该方法不推荐,因为这默认直接在源数据上做更改。
2).删除指定行
df.drop([0],axis=0)