【发布时间】:2019-10-05 18:24:00
【问题描述】:
我正在尝试规范化 pandas df 中的三位数国家代码列。我发现了一个很棒的函数country_converter,我目前正在一个非常大的数据框中的国家列上运行这个函数。由于列中存在NaN 值,它返回了 数千个 警告。
警告:root:nan 在 ISO3 中找不到
我正在寻找两件事:
- 专门抑制 nan 警告
- 加快此函数的处理时间(我的想法是抑制警告应该加快处理速度;但是,如果您对我的代码尝试不同的东西有任何建议,那就太好了!
我已经尝试了该名称的所有变体,但似乎没有任何效果,所以我认为我遗漏了一些东西......
import country_converter as coco
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings("ignore", message= "nan not found in ISO3")
warnings.filterwarnings("ignore", message= "root:nan not found in ISO3")
warnings.filterwarnings("ignore", message= "WARNING:root:nan not found in ISO3")
test = pd.DataFrame({"code":[np.nan, 'XXX', 'USA', 'GBR', "GBR",'SWE/n', "123", "abs", "ABCC", "ABC", np.nan, np.nan]})
test['code_convert']= test["code"].apply(lambda x: coco.convert(names= x, to='ISO3', not_found= np.NaN))
预计不会再看到带有 nan 值的警告。
【问题讨论】:
-
为什么不将该函数应用于值不为空的列?我们可以看看你的功能吗?
-
我在考虑那条路线,但我不知道该怎么做??如果我删除了空值,我以后将如何重新组合它们?上面的代码是我的实际代码,除了变量名。这是我的变量名的代码: ascent['country']= ascent["country"].apply(lambda x: coco.convert(names= x, to='ISO3', not_found= np.NaN))跨度>
标签: python python-3.x pandas warnings google-colaboratory