fillna:将缺失数据补全为0
get_dummies :将数据铺展开
df = pd.DataFrame([
['green' , 'A',1],
['red' , 'B',],
['blue' , 'A',3]])
df.columns = ['color', 'class','xx']
print(df)
df = df.fillna(0)
print(df)
df=pd.get_dummies(df,dummy_na = True)
pd.set_option('display.width',None)
print(df)
输出:
color class xx
0 green A 1.0
1 red B NaN
2 blue A 3.0
color class xx
0 green A 1.0
1 red B 0.0
2 blue A 3.0
xx color_blue color_green color_red color_nan class_A class_B class_nan
0 1.0 0 1 0 0 1 0 0
1 0.0 0 0 1 0 0 1 0
2 3.0 1 0 0 0 1 0 0