英文:
AttributeError: 'int' object has no attribute 'split' even when dtype of object is a string
问题
我有一个名为'state_lists'的列表,还有一个带有名为'ADDRESS'的列的数据帧。我想在我的数据帧中创建一个新列,方法如下:
- 遍历'ADDRESS'列
- 如果'ADDRESS'列中包含'state_lists'中的某个州名,则将该州名提取到名为'STATE'的另一列中
我之前使用了下面的代码,之前可以正常工作,但现在出现了上面提到的错误:
df["STATE"] = df["ADDRESS"].map(lambda x: state if (state:=x.split()[-1]) in state_lists else np.nan)
现在出现了什么问题?我已经检查了'ADDRESS'列的数据类型,它是一个对象。我确保已将所有空值替换为对象,但仍然出现相同的错误消息。
英文:
I have a list named 'state_lists' and I have a dataframe with column named 'ADDRESS'. I want to create a new column in my dataframe by
- iterating through the ADDRESS column
- if a state from the 'state_lists' is found in the ADDRESS column, the state is extrated into another column named STATE
I used this code below, before and and it worked but now it shows the error above
df["STATE"] = df["ADDRESS"].map(lambda x: state if ( state:=x.split()[-1]) in state_lists else np.nan)
What would be the problem now? I have checked the data type for the Address column and it is an object. This code has worked before dont know what is happening now?
I make sure I replaced all the null values with an object still the same error message pops up
答案1
得分: 0
你可以使用以下代码:
import re
pat = fr'\b({"|".join(map(re.escape, state_lists))})\b'
df["STATE"] = df["ADDRESS"].str.extract(pat, expand=False)
英文:
IIUC, you can use :
import re
pat = fr'\b({"|".join(map(re.escape, state_lists))})\b'
df["STATE"] = df["ADDRESS"].str.extract(pat, expand=False)
答案2
得分: 0
似乎您的字段ADDRESS
中有一个int
类型的值。您可以添加一个条件语句来检查ADDRESS
中的值是否是string
的实例,然后再应用split()
函数。您可以这样实现:
import numpy as np
def extract_state(address):
if isinstance(address, str):
state = address.split()[-1]
return state if state in state_lists else np.nan
else:
return np.nan
df["STATE"] = df["ADDRESS"].map(extract_state)
请注意,这里的代码仅涉及翻译,不包含其他内容。
英文:
Seems you have int
in your field ADDRESS
. You can add a conditional statement to check if the value in ADDRESS
is an instance of string
before applying split()
function. You can implement it as:
import numpy as np
def extract_state(address):
if isinstance(address, str):
state = address.split()[-1]
return state if state in state_lists else np.nan
else:
return np.nan
df["STATE"] = df["ADDRESS"].map(extract_state)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论