gpt4 book ai didi

python - Numpy的where函数和长度报错信息

转载 作者:行者123 更新时间:2023-12-02 15:56:15 25 4
gpt4 key购买 nike

我有一个要更正的电子表格。在 Billing Categorization 上,应按适用填写“标准”或“非标准”。

我正在尝试使用 numpy 中的 where 函数来执行此操作:

df['Billing Categorization'] = np.where((df['Billing Categorization'].isnull(), ~df['AE Number'].isnull()), 'Standard', df['Billing Categorization'])

想法是 Billing Categorization 中的空值应该用“Standard”填充,其中在同一行中“AE Number”列中的值不为空。

但是,我收到以下错误:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-64-863f807f354c> in <module>
30 df.loc[df["PQC-Product"].isnull(),'PQC-Product'] = df["Request-Product"]
31
---> 32 df['Billing Categorization'] = np.where((df['Billing Categorization'].isnull(), ~df['AE Number'].isnull()), 'Standard', df['Billing Categorization'])
33
34 #We simply get the data out

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
3161 else:
3162 # set column
-> 3163 self._set_item(key, value)
3164
3165 def _setitem_slice(self, key: slice, value):

~\Anaconda3\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value)
3240 """
3241 self._ensure_valid_index(value)
-> 3242 value = self._sanitize_column(key, value)
3243 NDFrame._set_item(self, key, value)
3244

~\Anaconda3\lib\site-packages\pandas\core\frame.py in _sanitize_column(self, key, value, broadcast)
3897
3898 # turn me into an ndarray
-> 3899 value = sanitize_index(value, self.index)
3900 if not isinstance(value, (np.ndarray, Index)):
3901 if isinstance(value, list) and len(value) > 0:

~\Anaconda3\lib\site-packages\pandas\core\internals\construction.py in sanitize_index(data, index)
749 """
750 if len(data) != len(index):
--> 751 raise ValueError(
752 "Length of values "
753 f"({len(data)}) "

ValueError: Length of values (2) does not match length of index (876)

两列都有空值,但我只想填充适用的值。显然,并非所有这些都是可能的。我想从这里开始:

<表类="s-表"><头>数账单分类国家AE 编号AE国家<日>日期 <正文>首先NaN意大利55568意大利2022 年 1 月 1 日第二个NaN法国NaNNaNNaN第三次标准西类牙85968西类牙2022 年 1 月 5 日第四期非标准英国748265英国2022 年 1 月 5 日第五名标准英国59632英国2022 年 1 月 6 日第六名NaN英国78946英国22 年 1 月 7 日

给这个:

<表类="s-表"><头>数账单分类国家AE 编号AE国家<日>日期 <正文>首先标准意大利55568意大利2022 年 1 月 1 日第二个NaN法国NaNNaNNaN第三次标准西类牙85968西类牙2022 年 1 月 5 日第四期非标准英国748265英国2022 年 1 月 5 日第五名标准英国59632英国2022 年 1 月 6 日第六名标准英国78946英国22 年 1 月 7 日

正如您在第二行看到的那样,由于没有 AE 编号,因此不应更改任何内容,因为它应保持空白。我已经手动检查了两列的长度并且它们匹配,所以有什么问题吗?

最佳答案

& 的 IIUC 链掩码:

m = df['Billing Categorization'].isna() & df['AE Number'].notna()
df['Billing Categorization'] = np.where(m, 'Standard', df['Billing Categorization'])

关于python - Numpy的where函数和长度报错信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71468468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com