在熊猫中使用groupby在模式下替换缺失值时出现IndexError

时间:2019-01-01 10:12:03

标签: python pandas dataframe pandas-groupby missing-data

我有一个数据集,需要缺失值处理。

 Column                      Missing Values

 Complaint_ID                    0         
 Date_received                   0         
 Transaction_Type                0         
 Complaint_reason                0         
 Company_response              22506         
 Date_sent_to_company            0         
 Complaint_Status                0         
 Consumer_disputes             7698

现在的问题是,当我尝试使用values用其他columns的模式替换缺少的groupby时:

代码:

data11["Company_response"] = 
data11.groupby("Complaint_reason").transform(lambda x: x.fillna(x.mode() 
[0]))["Company_response"]

data11["Consumer_disputes"] = 
data11.groupby("Transaction_Type").transform(lambda x: x.fillna(x.mode() 
[0]))["Consumer_disputes"]

我收到以下错误:

Stacktrace

Traceback (most recent call last):

File "<ipython-input-89-8de6a010a299>", line 1, in <module>
    data11["Company_response"] = data11.groupby("Complaint_reason").transform(lambda x: x.fillna(x.mode()[0]))["Company_response"]

  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 3741, in transform
    return self._transform_general(func, *args, **kwargs)

  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 3699, in _transform_general
    res = path(group)

  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 3783, in <lambda>
    lambda x: func(x, *args, **kwargs), axis=self.axis)

  File "C:\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4360, in apply
    ignore_failures=ignore_failures)

  File "C:\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4456, in _apply_standard
    results[i] = func(v)

  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 3783, in <lambda>
    lambda x: func(x, *args, **kwargs), axis=self.axis)

  File "<ipython-input-89-8de6a010a299>", line 1, in <lambda>
    data11["Company_response"] = data11.groupby("Complaint_reason").transform(lambda x: x.fillna(x.mode()[0]))["Company_response"]

  File "C:\Anaconda3\lib\site-packages\pandas\core\series.py", line 601, in __getitem__
    result = self.index.get_value(self, key)

  File "C:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2434, in get_value
    return libts.get_value_box(s, key)

  File "pandas\_libs\tslib.pyx", line 923, in pandas._libs.tslib.get_value_box (pandas\_libs\tslib.c:18843)

  File "pandas\_libs\tslib.pyx", line 939, in pandas._libs.tslib.get_value_box (pandas\_libs\tslib.c:18560)

IndexError: ('index out of bounds', 'occurred at index Consumer_disputes')

我已经检查了length中的dataframe及其所有列,它是相同的:43266。

我也找到了与此类似的问题,但没有正确的答案:Click here

请帮助解决该错误。

  

IndexError :(“索引超出范围”,“发生在索引Consumer_disputes上”)

如果有任何帮助,以下是数据集的快照:Dataset Snapshot

我正在成功使用以下代码。但这并不完全符合我的目的。有助于填补缺失的值。

data11['Company_response'].fillna(data11['Company_response'].mode()[0], 
inplace=True)
data11['Consumer_disputes'].fillna(data11['Consumer_disputes'].mode()[0], 
inplace=True)

Edit1 :(附加示例)

输入给定: InputImage

预期输出: OutputImage

您可以看到,通过采用投诉原因模式填充了Tr-1和Tr-3公司响应的缺失值。 对于Tr-5,对于采用交易类型模式的“消费者争议”也是如此。

下面的代码段由数据框和想要复制并尝试一下的代码组成。

复制代码

import pandas as pd
import numpy as np

data11=pd.DataFrame({'Complaint_ID':['Tr-1','Tr-2','Tr-3','Tr-4','Tr-5','Tr-6'],
                    'Transaction_Type':['Mortgage','Credit card','Bank account or service','Debt collection','Credit card','Mortgage'],
                    'Complaint_reason':['Loan servicing, payments, escrow account','Incorrect information on credit report',"Cont'd attempts collect debt not owed","Cont'd attempts collect debt not owed",'Payoff process','Loan servicing, payments, escrow account'],
                    'Company_response':[np.nan,'Company chooses not to provide a public response',np.nan,'Company believes it acted appropriately as authorized by contract or law','Company has responded to the consumer and the CFPB and chooses not to provide a public response','Company disputes the facts presented in the complaint'],
                    'Consumer_disputes':['Yes','No','No','No',np.nan,'Yes']})

data11.isnull().sum()

data11["Company_response"] = data11.groupby("Complaint_reason").transform(lambda x: x.fillna(x.mode()[0]))["Company_response"]
data11["Consumer_disputes"] = data11.groupby("Transaction_Type").transform(lambda x: x.fillna(x.mode()[0]))["Consumer_disputes"]    

3 个答案:

答案 0 :(得分:2)

引发该错误是因为对于至少一组而言,相应的聚合列中的值仅包含np.nan值。在这种情况下,pd.Series([np.nan]).mode()返回一个空序列,当您采用第一个值时会导致错误。

因此,您可以使用类似transform(lambda x: x.fillna(x.mode()[0] if not x.mode().empty else "Empty") )的东西。

答案 1 :(得分:1)

尝试:

data11["Company_response"] = data11.groupby("Complaint_reason")['Company_response'].transform(lambda x: x.fillna(x.mode()[0]))

data11["Consumer_disputes"] = data11.groupby("Transaction_Type")['Consumer_disputes'].transform(lambda x: x.fillna(x.mode()[0]))  

答案 2 :(得分:0)

@Mikhail Berlinkov几乎可以肯定是正确的。我能够重现您的错误,然后通过使用dropna()来避免它:

data11.groupby("Transaction-Type").transform(
    lambda x: x.fillna(x.mode() [0]))["Consumer-disputes"]  
# Returns IndexError

data11.dropna().groupby("Transaction-Type").transform(
    lambda x: x.fillna(x.mode() [0]))["Consumer-disputes"]  
# Works