Панды применяют функцию, возвращающую список из 4-х элементов к ошибке в 4-х столбцах - PullRequest
0 голосов
/ 06 мая 2018

Я пытаюсь применить функцию к столбцу в моем 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"

Попытка вернуть набор вместо списка, не сработала.

1 Ответ

0 голосов
/ 06 мая 2018

Я быстро собрал небольшое предложение, которое может быть тем, что вы ищете. Позвольте мне знать, если это помогает.

from pandas import DataFrame
import re

def separate_reagan(row):
    # row is a single row from the dataframe which is what is passed in
    # from df.apply(fcn, axis=1)
    # note: this means that you can also set values on the row

    # switch local variables to setting row in dataframe if you
    # really want to initialize them. If they are missing they should 
    # just become some form of NaN or None though depending on the dtype
    row['township'] = None
    row['section'] = None
    row['acres'] = None
    row['block'] = None

    # grab legal description here instead of passing it in as the only variable
    data = row['legal_description']
    if 'BLK' in data:
        block_list = re.search(r'BLK (\d{1,3})', data)
        if block_list:
            row['block'] = block_list.group(1)
    else:
        # since you only seem to want the first match, 
        # search is probably more what you're looking for
        block_list = re.search(r'B-([0-9]{1,3})', data)
        if block_list:
            row['block'] = block_list.group(1)

    # Similar for others

    # returns the modified row.
    return row

df = DataFrame([
    {'id': 1, 'legal_description': '43560 CLARKSON | ENDEAVOR ENERGY RESO | A- ,B...'},
    {'id': 2, 'legal_description': '4143990 CLARKSON ESTATE | ENDEAVOR ENERGY RESO ...'},
    {'id': 3, 'legal_description': '144420 CLARKSON RANCH | ENDEAVOR ENERGY RESO |...'},
])
df = df[['id','legal_description']]

# df now only has columns ID and Legal Description

# This left hand side gets the columns from the dataframe, but as mentioned in the comment
# above, those columns in not contained in the dataframe. Also they aren't returned from the 
# apply function because you never set them in separateReagan

df = df.apply(separate_reagan, axis=1)
# now these columns exist because you set them in the function
print(df[['block','township','section','acres']])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...