插补数据导致ValueError:替换长度必须等于序列长度

2024-05-15 01:58:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含以下列的数据框

missing_df.columns.tolist()

返回一个列表:

   ['order_id',
     'customer_id',
     'date',
     'nearest_warehouse',
     'shopping_cart',
     'order_price',
     'delivery_charges',
     'customer_lat',
     'customer_long',
     'coupon_discount',
     'order_total',
     'season',
     'is_expedited_delivery',
     'distance_to_nearest_warehouse',
     'latest_customer_review',
     'is_happy_customer',
     'Autumn',
     'Spring',
     'Summer',
     'Winter',
     'exp_int']

如果我尝试运行以下操作

missing_df['delivery_charges'][missing_df['delivery_charges'].isnull()] = iimput_model.predict(missing_df.drop(['order_id','customer_id','date','nearest_warehouse','shopping_cart','order_price','delivery_charges','customer_lat','customer_long','coupon_discount','order_total','season','is_expedited_delivery','latest_customer_review'],1))

我得到以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Roaming\Python\Python37\site-packages\pandas\core\series.py in __setitem__(self, key, value)
    999         try:
-> 1000             self._set_with_engine(key, value)
   1001         except (KeyError, ValueError):

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\series.py in _set_with_engine(self, key, value)
   1032         # fails with AttributeError for IntervalIndex
-> 1033         loc = self.index._engine.get_loc(key)
   1034         validate_numeric_casting(self.dtype, value)

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

TypeError: '0      False
1      False
2      False
3       True
4      False
       ...  
495    False
496    False
497    False
498    False
499    False
Name: delivery_charges, Length: 500, dtype: bool' is an invalid key

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-418-60aee038ac4a> in <module>
      1 #dirty_df.reindex(columns=filtered_columns)
      2 #missing_df['delivery_charges'][missing_df['delivery_charges'].isnull()] = iimput_model.predict(missing_df.drop(['order_id','customer_id','date','nearest_warehouse','shopping_cart','order_price','delivery_charges','customer_lat','customer_long','coupon_discount','order_total','season','is_expedited_delivery','latest_customer_review'],1))
----> 3 missing_df['delivery_charges'][missing_df['delivery_charges'].isnull()] = iimput_model.predict(missing_df.reindex(columns=filter_cols))

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\series.py in __setitem__(self, key, value)
   1018                 key = np.asarray(key, dtype=bool)
   1019                 try:
-> 1020                     self._where(~key, value, inplace=True)
   1021                 except InvalidIndexError:
   1022                     self.iloc[key] = value

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\generic.py in _where(self, cond, other, inplace, axis, level, errors, try_cast)
   8820                     else:
   8821                         raise ValueError(
-> 8822                             "Length of replacements must equal series length"
   8823                         )
   8824 

ValueError: Length of replacements must equal series length

有人能解释一下这个错误以及如何解决它吗?我在这个问题上花了一点时间,并且一直在缩短

谢谢你的建议或意见


Tags: keyinselfidfalsepandasdfis
1条回答
网友
1楼 · 发布于 2024-05-15 01:58:17

这是因为在等号的两边有两个不同长度的序列。这是因为您在左侧根据空值对序列进行子集划分,而在右侧不这样做。你只需要确保你的系列长度相等

相关问题 更多 >

    热门问题