使用any(1)的pandas突然开始出错?

问题描述 投票:0回答:2

我的代码运行完美,当我尝试时我更新了

openpyxl

data = {'Col1': ['Charges', 'Realized P&L', 'Other Credit & Debit', 'Some Other Value'],
        'Col2': [100, 200, 300, 400],
        'Col3': ['True', False, 'True', 'False']}
df = pd.DataFrame(data)

# keep rows where certain charges etc are present
filtered_df = df[df.isin(["Charges", "Realized P&L", "Other Credit & Debit"]).any(1)]

我收到错误:

Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
TypeError: any() takes 1 positional argument but 2 were given

我尝试过:

filtered_df = df[df.isin(["Charges", "Realized P&L", "Other Credit & Debit"]).any()]
# removed 1 from any
UserWarning: Boolean Series key will be reindexed to match DataFrame index.
Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 3751, in __getitem__
    return self._getitem_bool_array(key)
  File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/core/frame.py", line 3804, in _getitem_bool_array
    key = check_bool_indexer(self.index, key)
  File "/Volumes/coding/venv/lib/python3.8/site-packages/pandas/core/indexing.py", line 2499, in check_bool_indexer
    raise IndexingError(
pandas.errors.IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match).

不确定发生了什么或为什么它突然停止工作。

python python-3.x pandas
2个回答
2
投票

TL;DR:重点是

1
以前是位置参数,现在是关键字参数,因此您必须显式指定
any(axis=1)

说明:

版本1.4.4

docu
指出:

DataFrame.any(axis=0, bool_only=None, skipna=True, level=None, **kwargs)

1.5.2 版本的 docu 指出:

DataFrame.any(*, axis=0, bool_only=None, skipna=True, level=None, **kwargs)

axis 参数之前的

*
表示其后面的所有参数必须指定为关键字参数,而不是位置参数。所以看起来版本
1.4
1.5
之间的所有参数都从位置参数切换为关键字参数。


1
投票

我认为更正确的过滤方法是:

filtered_df = df[df['Col1'].isin(["Charges", "Realized P&L", "Other Credit & Debit"])]

或者明确指定

axis
参数

filtered_df = df[df.isin(["Charges", "Realized P&L", "Other Credit & Debit"]).any(axis=1)]
© www.soinside.com 2019 - 2024. All rights reserved.