我正在尝试对df中的一列应用一个函数,并根据返回的列表添加4个新列。你知道吗
下面是返回列表的函数。你知道吗
def separateReagan(data):
block = None
township = None
section = None
acres = None
if 'BLK' in data:
patern = r'BLK (\d{1,3})'
blockList = re.findall(patern,data)
if blockList:
block = blockList[0]
else:
patern = r'B-([0-9]{1,3})'
blockList = re.findall(patern,data)
if blockList:
block = blockList[0]
# Similar for others
return [block,township,section,acres]
这是数据帧的代码。你知道吗
df = df[['ID','Legal Description']]
# Dataframe looks like this
# ID Legal Description
# 0 1 143560 CLARKSON | ENDEAVOR ENERGY RESO | A- ,B...
# 1 2 143990 CLARKSON ESTATE | ENDEAVOR ENERGY RESO ...
# 2 3 144420 CLARKSON RANCH | ENDEAVOR ENERGY RESO |...
df[['Block','Township','Section','Acres']] = df.apply(lambda x: separateReagan(x['Legal Description']),axis=1)
我得到这个错误:
KeyError: "['Block' 'Township' 'Section' 'Acres'] not in index"
尝试返回一个元组而不是列表,但不起作用。你知道吗
我很快就提出了一个小建议,也许正是你想要的。如果这有帮助,请告诉我。你知道吗
相关问题 更多 >
编程相关推荐