【问题标题】:Can't detect NaN values in dataframe (python)无法检测数据框中的 NaN 值(python)
【发布时间】:2021-03-09 20:06:16
【问题描述】:

我有一个从 osmnx 获得的地理数据框:

cities = ox.geocode_to_gdf(['Município de Lisboa', 'Município de Oeiras', 'Município da Amadora', 'Município de Loures', 'Município de Odivelas'])
whole_polygon = cities.unary_union #unary union of both geometries
G = ox.graph_from_polygon(whole_polygon, network_type='drive', simplify=True)
G_nx = nx.relabel.convert_node_labels_to_integers(G)

nodes, edges = ox.graph_to_gdfs(G_nx, nodes=True, edges =  True)

我试图确定name 列的值何时为NaN,但由于某种原因它无法检测到:

for i in range(len(edges)):

    if edges['name'].iloc[i] == float('nan'):
        print('here')

我尝试过float('NaN'),而不是代码中的内容。我检查了当name 值为NaN 时,该变量确实是一个浮点数。名称列中的值共有三种类型:liststrfloat,其中浮点数仅在值为NaN时才会出现

【问题讨论】:

  • if edges['name'].isnull(): print('here') 这个有用吗
  • 那行不通
  • if edges['name'].iloc[i].isnull(): print('here') 忘了iloc

标签: python pandas osmnx


【解决方案1】:
data = {"h":[1,np.nan,2]}
pd.DataFrame(data).isnull()

#output
    h
0   False
1   True
2   False

【讨论】:

  • 哦,我明白了,确实有效。我忘记了数据实际上可能丢失了,而不是实际的浮点数
猜你喜欢
  • 2020-03-18
  • 2018-07-21
  • 2016-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多