• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共95篇

    python3.X - 数据分析 - Pandas

关闭

返回栏目

关闭

返回python3.X - 数据分析 - Pandas栏目

55 - 内容 - df.fillna() - NaN转换

作者:

贺及楼

成为作者

更新日期:2024-03-24 11:58:43

df.fillna()NaN转换

NaN转0 df.fillna(0)

  1. import numpy as np
  2. import pandas as pd
  3. # 全部列
  4. df = df.fillna(0)
  5. df.fillna(0, inplace=True)
  6. # 2句区别是新增与不新增
  7. # 指定列
  8. trans={"A":9,"B":8,"C":7,"D":6}
  9. df.fillna(value=trans)
  1. data_df = pd.DataFrame(data=[{"A":"bike", "B":"bike"},{"A":"car", "B":np.nan},{"A":"bus", "B":np.nan}])
A B
0 bike bike
1 car NaN
2 bus NaN
  1. df = df.fillna(0)
A B
0 bike bike
1 car 0
2 bus 0

B列NAN空值由A列填补

  1. data_df["B"].fillna(data_df["A"], inplace=True)
  1. data_df = pd.DataFrame(data=[{"A":"bike", "B":"bike"},{"A":"car", "B":np.nan},{"A":"bus", "B":np.nan}])
A B
0 bike bike
1 car NaN
2 bus NaN
  1. data_df["B"].fillna(data_df["A"], inplace=True)
A B
0 bike bike
1 car car
2 bus bus