AttributeError: ‘int’对象没有属性’split’,即使对象的数据类型是字符串。

huangapple go评论74阅读模式
英文:

AttributeError: 'int' object has no attribute 'split' even when dtype of object is a string

问题

我有一个名为'state_lists'的列表,还有一个带有名为'ADDRESS'的列的数据帧。我想在我的数据帧中创建一个新列,方法如下:

  1. 遍历'ADDRESS'列
  2. 如果'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

  1. iterating through the ADDRESS column
  2. 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)


huangapple
  • 本文由 发表于 2023年6月19日 23:39:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76508151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定