• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共95篇

    python3.X - 数据分析 - Pandas

关闭

返回栏目

关闭

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

56 - 内容 - python语法判空

作者:

贺及楼

成为作者

更新日期:2024-12-05 10:36:29

语法判空

例子

  1. import pandas as pd
  2. import numpy as np
  3. data = [{"a":"nan","b":np.nan,"c":None,}]
  4. df = pd.DataFrame(data=data, )
  5. print(df)

空的控制台打印显示

“nan”

  1. print("-------a-------")
  2. print(df.iloc[0,:]['a'])
  3. print(type(df.iloc[0,:]['a']))
  4. if df.iloc[0,:]['a']:
  5. print("a") # 到此
  6. if df.iloc[0,:]['a'] == "nan":
  7. print("a2") # 到此
  8. if df.iloc[0,:]['a'] == np.nan:
  9. print("a3")
  10. if df.iloc[0,:]['a'] == "None":
  11. print("a4")
  12. if df.iloc[0,:]['a'] == None:
  13. print("a5")

np.nan

  1. print("-------b-------")
  2. print(df.iloc[0,:]['b'])
  3. print(type(df.iloc[0,:]['b']))
  4. if df.iloc[0,:]['b']:
  5. print("b") # 到此
  6. if df.iloc[0,:]['b'] == "nan":
  7. print("b2")
  8. if df.iloc[0,:]['b'] == np.nan:
  9. print("b3")
  10. if df.iloc[0,:]['b'] == "None":
  11. print("b4")
  12. if df.iloc[0,:]['b'] == None:
  13. print("b5")

None

  1. print("-------c-------")
  2. print(df.iloc[0,:]['c'])
  3. print(type(df.iloc[0,:]['c']))
  4. if df.iloc[0,:]['c']:
  5. print("c")
  6. if df.iloc[0,:]['c'] == "nan":
  7. print("c2")
  8. if df.iloc[0,:]['c'] == np.nan:
  9. print("c3")
  10. if df.iloc[0,:]['c'] == "None":
  11. print("c4")
  12. if df.iloc[0,:]['c'] == None:
  13. print("c5") # 到此

  1. data = Series['列名']
  2. if pd.isnull(data):
  3. return "空的"