编译器问题:给定带有字符串和列单元格的 T 或 F 条件的 replace() 上的 AssertionError
Compiler issue: AssertionError on replace( ) given T or F condition with string and column cell
我想检查列条目是否与城市(区域)列表中的城市匹配,如果匹配,则我想向列中添加一个带有区域邮政编码的字符串(region_name) 如果它不匹配,那么我想保留当前列值。
案例回顾
- https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.replace.html and https://www.w3resource.com/pandas/series/series-replace.php 它在这里说,“如果正则表达式不是 bool 并且 to_replace 不是 None,则引发 AssertionError。”我不是 100% 清楚那是什么意思。
我尝试了一个新库 (modin) 并进行了一些更改(包括根据弹出窗口的提示安装 pylint),之后 replace() 不再适用于列。
import pandas as pd
df = pd.DataFrame({'city_nm': ['Cupertino', 'Mountain View', 'Palo Alto'],'zip_cd': ['95014', False, '94306']})
region_name = '99999'
region = ['Cupertino', 'Mountain View', 'Palo Alto']
def InferZipcodeFromCityName(df, region, region_name):
PATTERN_CITY = '|'.join(region)
foundZipbyCity = (
(df['zip_cd'] == False) &
(df['cty_nm'].str.contains(PATTERN_CITY, flags=re.IGNORECASE) )
)
df['zip_cd'] = foundZipbyCity.replace( (True,False), (region_name, df['zip_cd']) )
return df
#this is what I want
In[1]: df = InferZipcodeFromCityName(df, region, region_name)
Out[1]:
city_nm zip_cd
0 'Cupertino' '95014'
1 'Mountain View' '99999'
2 'Palo Alto' '94306'
#this is what I get --> AssertionError
try 1: df['zip_cd'] = foundZipbyCity.replace( (True,False), (region_name, df['zip_cd']), regex = False ) #AssertionError
try 2: df['zip_cd'] = foundZipbyCity.replace( (True,False), (region_name, region_name]) ) #changed to (string,string) and works fine, however, it does nothing useful
编辑:在第二台和第三台笔记本电脑上,我安装了 Anaconda 和 VS Code,它运行良好:在第一台笔记本电脑上,我卸载了 anaconda 和 VS Code,然后重新安装没有效果(这台笔记本电脑使用这段代码运行良好一年,直到我尝试了 modin 库——可能是巧合,但仍然如此)
问题是您期望在此语句中所有 False
值将从 df["zip_cd"]
:
中获取
df['zip_cd'] = foundZipbyCity.replace( (True, False), (region_name, df['zip_cd']) )
然而这不是真的,这里发生的是我们将尝试将 False 替换为系列 False -> df["zip_cd"]
而 pandas 似乎无法将 False 标量替换为系列。
您可能想在这里做的是将 df["zip_cd"]
中满足 foundZipbyCity
掩码的所有值替换为 region_name
df["zip_cd"][foundZipbyCity] = region_name
我已经 运行 您的代码进行了此更改,它输出了预期的结果。
我想检查列条目是否与城市(区域)列表中的城市匹配,如果匹配,则我想向列中添加一个带有区域邮政编码的字符串(region_name) 如果它不匹配,那么我想保留当前列值。
案例回顾
- https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.replace.html and https://www.w3resource.com/pandas/series/series-replace.php 它在这里说,“如果正则表达式不是 bool 并且 to_replace 不是 None,则引发 AssertionError。”我不是 100% 清楚那是什么意思。
我尝试了一个新库 (modin) 并进行了一些更改(包括根据弹出窗口的提示安装 pylint),之后 replace() 不再适用于列。
import pandas as pd
df = pd.DataFrame({'city_nm': ['Cupertino', 'Mountain View', 'Palo Alto'],'zip_cd': ['95014', False, '94306']})
region_name = '99999'
region = ['Cupertino', 'Mountain View', 'Palo Alto']
def InferZipcodeFromCityName(df, region, region_name):
PATTERN_CITY = '|'.join(region)
foundZipbyCity = (
(df['zip_cd'] == False) &
(df['cty_nm'].str.contains(PATTERN_CITY, flags=re.IGNORECASE) )
)
df['zip_cd'] = foundZipbyCity.replace( (True,False), (region_name, df['zip_cd']) )
return df
#this is what I want
In[1]: df = InferZipcodeFromCityName(df, region, region_name)
Out[1]:
city_nm zip_cd
0 'Cupertino' '95014'
1 'Mountain View' '99999'
2 'Palo Alto' '94306'
#this is what I get --> AssertionError
try 1: df['zip_cd'] = foundZipbyCity.replace( (True,False), (region_name, df['zip_cd']), regex = False ) #AssertionError
try 2: df['zip_cd'] = foundZipbyCity.replace( (True,False), (region_name, region_name]) ) #changed to (string,string) and works fine, however, it does nothing useful
编辑:在第二台和第三台笔记本电脑上,我安装了 Anaconda 和 VS Code,它运行良好:在第一台笔记本电脑上,我卸载了 anaconda 和 VS Code,然后重新安装没有效果(这台笔记本电脑使用这段代码运行良好一年,直到我尝试了 modin 库——可能是巧合,但仍然如此)
问题是您期望在此语句中所有 False
值将从 df["zip_cd"]
:
df['zip_cd'] = foundZipbyCity.replace( (True, False), (region_name, df['zip_cd']) )
然而这不是真的,这里发生的是我们将尝试将 False 替换为系列 False -> df["zip_cd"]
而 pandas 似乎无法将 False 标量替换为系列。
您可能想在这里做的是将 df["zip_cd"]
中满足 foundZipbyCity
掩码的所有值替换为 region_name
df["zip_cd"][foundZipbyCity] = region_name
我已经 运行 您的代码进行了此更改,它输出了预期的结果。