Как устранить ошибку типа при использовании функции pandas apply с лямбда-выражением - PullRequest
0 голосов
/ 02 апреля 2019

Я пишу интеллектуальное приложение, которое определяет, какие факторы приводят к 0 детям в отношениях, основываясь на данных из набора данных по выбору методов контрацепции UCI Machine Learning Repository, приведенного в Dua, D. and Graff, C. (2019). Репозиторий машинного обучения UCI [http://archive.ics.uci.edu/ml]. Ирвин, Калифорния: Калифорнийский университет, Школа информации и компьютерных наук. У меня проблемы с написанием лямбда-выражения с помощью функции применения панд.

Я не уверен, что попробовать.

Вот пример файла

wife's age, wife's education, husband's education, number of children, wife's religion, wife now working, husband's occupation, standard-of-living index, media exposure, contraceptive method used
24,2,3,3,1,1,2,3,0,1
45,1,3,10,1,1,3,4,0,1
43,2,3,7,1,1,3,4,0,1
42,3,2,9,1,1,3,3,0,1
36,3,3,8,1,1,3,2,0,1
19,4,4,0,1,1,3,3,0,1

и вот мой код

#import modules
import pandas as pd

#define functions
def read_datafile():
    d = pd.read_csv('cmc.data.txt', sep=',')
    return d

def create_bin_label(data):
    data['numchildren'] = data.apply(lambda row: 1 if (row['number of children']) <= 0 else 0, axis=1)
    data = data.drop(['number of children'], axis=1)

#read in datafile
data = read_datafile()
print(len(data))

#create a binary label column and delete the old column
bl = create_bin_label(data)
print(data.head())

Я ожидаю, что create_bin_label (data) изолирует одно значение от набора числовых значений, найденных в числовом атрибуте ex). Число детей может быть любым числом, но я хочу только 0, я также ожидаю, что он добавит столбец "numchildren" как двоичная метка, и я ожидаю, что create_bin_label (data) удалит старый столбец (он называется «число детей»). То, что создает create_bin_label (data), возвращает ошибку, которая выглядит следующим образом (хотя я думаю, что важная часть заключается в том, что некоторые str пытается обрабатываться как int, но я не уверен, где это происходит)

Traceback (most recent call last):
  File "C:\Users\Hezekiah\PycharmProjects\Artificial Intelligence 0\venv\lib\site-packages\pandas\core\indexes\base.py", line 4381, in get_value
    return libindex.get_value_box(s, key)
  File "pandas\_libs\index.pyx", line 52, in pandas._libs.index.get_value_box
  File "pandas\_libs\index.pyx", line 48, in pandas._libs.index.get_value_at
  File "pandas\_libs\util.pxd", line 113, in pandas._libs.util.get_value_at
  File "pandas\_libs\util.pxd", line 98, in pandas._libs.util.validate_indexer
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Hezekiah/PycharmProjects/Artificial Intelligence 0/Chapter 1 Application Contraception.py", line 24, in <module>
    bl = create_bin_label(data)
  File "C:/Users/Hezekiah/PycharmProjects/Artificial Intelligence 0/Chapter 1 Application Contraception.py", line 14, in create_bin_label
    data['numchildren'] = data.apply(lambda row: 1 if (row['number of children']) <= 0 else 0, axis=1)
  File "C:\Users\Hezekiah\PycharmProjects\Artificial Intelligence 0\venv\lib\site-packages\pandas\core\frame.py", line 6487, in apply
    return op.get_result()
  File "C:\Users\Hezekiah\PycharmProjects\Artificial Intelligence 0\venv\lib\site-packages\pandas\core\apply.py", line 151, in get_result
    return self.apply_standard()
  File "C:\Users\Hezekiah\PycharmProjects\Artificial Intelligence 0\venv\lib\site-packages\pandas\core\apply.py", line 257, in apply_standard
    self.apply_series_generator()
  File "C:\Users\Hezekiah\PycharmProjects\Artificial Intelligence 0\venv\lib\site-packages\pandas\core\apply.py", line 286, in apply_series_generator
    results[i] = self.f(v)
  File "C:/Users/Hezekiah/PycharmProjects/Artificial Intelligence 0/Chapter 1 Application Contraception.py", line 14, in <lambda>
    data['numchildren'] = data.apply(lambda row: 1 if (row['number of children']) <= 0 else 0, axis=1)
  File "C:\Users\Hezekiah\PycharmProjects\Artificial Intelligence 0\venv\lib\site-packages\pandas\core\series.py", line 868, in __getitem__
    result = self.index.get_value(self, key)
  File "C:\Users\Hezekiah\PycharmProjects\Artificial Intelligence 0\venv\lib\site-packages\pandas\core\indexes\base.py", line 4389, in get_value
    raise e1
  File "C:\Users\Hezekiah\PycharmProjects\Artificial Intelligence 0\venv\lib\site-packages\pandas\core\indexes\base.py", line 4375, in get_value
    tz=getattr(series.dtype, 'tz', None))
  File "pandas\_libs\index.pyx", line 81, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 89, in pandas._libs.index.IndexEngine.get_value
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('number of children', 'occurred at index 0')

1 Ответ

1 голос
/ 02 апреля 2019
import pandas as pd

#define functions
def read_datafile():
    d = pd.read_csv('cmc.data.txt', sep=',')
    return d

def create_bin_label(data,columns):
    # i added an extra columns argument that holds a list of all column names 
    # the 'number of children' column is on position 3 in the list
    data['numchildren'] = data.apply(lambda row: 1 if (row[columns[3]]) <= 0 else 0, 
                           axis=1)
    data = data.drop([columns[3]], axis=1)

#read in datafile
data = read_datafile()
print(len(data))
columns = data.columns.values #this creates the list of the dataframe's column names

#create a binary label column and delete the old column
bl = create_bin_label(data,columns) # remember to insert the var that holds the cols
print(data)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...