禁止显示警告-python-pandas-colab_notebook

时间:2019-05-18 21:36:09

标签: python python-3.x pandas warnings google-colaboratory

我正在尝试对熊猫df中的三位数国家/地区代码列进行规范化。我找到了一个名为<style> @font-face { font-family: myFirstFont; src: url(fonts/Gotham Light Regular/Gotham Light Regular.otf); } </style> <div class= "name"> <h1>Hello</h1> <h2>Bye</h2> </div>的出色函数,目前我正在非常大的数据框中的country列上运行此函数。由于这些列中存在country_converter个值,因此会返回数千个警告。

  

警告:在ISO3中找不到root:nan

我正在寻找两件事:

  1. 专门禁止显示南警告
  2. 加快此功能的处理时间(我认为抑制警告可以加快处理速度;但是,如果您有任何建议尝试使用与我的代码不同的东西,那就更好了!

我已经尝试过所有名称的变体,但是似乎没有任何作用,所以我认为我缺少一些东西...

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)) 值的警告。

1 个答案:

答案 0 :(得分:1)

我已经调整了数据框中的数据,使n​​p.nan成为正确的np.nan而不是字符串。

test = pd.DataFrame(
    {
        "code": [
            np.nan,
            "XXX",
            "USA",
            "GBR",
            "GBR",
            "SWE/n",
            "123",
            "abs",
            "ABCC",
            "ABC",
            np.nan,
            np.nan,
        ]
    }
)

print(test)

     code
0     NaN
1     XXX
2     USA
3     GBR
4     GBR
5   SWE/n
6     123
7     abs
8    ABCC
9     ABC
10    NaN
11    NaN

那么您要做的就是在进行计算时过滤掉np.nan。

test["code_convert"] = test[test.notna()].apply(
    lambda x: coco.convert(names=x, to="ISO3")
)

我没有安装国家/地区转换器,但是如果我简化了申请测试:

test["code_convert"] = test[test.notna()].apply(
    lambda x: x + "_solution"
)

print(test)

     code    code_convert
0     NaN             NaN
1     XXX    XXX_solution
2     USA    USA_solution
3     GBR    GBR_solution
4     GBR    GBR_solution
5   SWE/n  SWE/n_solution
6     123    123_solution
7     abs    abs_solution
8    ABCC   ABCC_solution
9     ABC    ABC_solution
10    NaN             NaN
11    NaN             NaN
相关问题