英文:
TypeError: DataFrame.drop() takes from 1 to 2 positional arguments but 3 were given
问题
以下是翻译好的部分:
我有一个大文件,我试图使用 dataframe.drop 函数来减小它的大小。以下是我的代码:
probe_df = pd.read_csv(csv_file, header=9233, nrows=4608)
# 去除不需要的列
c_to_drop = 'Unnamed: ' + str(count_tp+1)
probe_df = probe_df.set_index("Chamber ID").drop(c_to_drop, 1)
当我运行这个代码时,我收到了以下错误信息:
probe_df = probe_df.set_index("Chamber ID").drop(c_to_drop, 1)
TypeError: DataFrame.drop() 接受 1 到 2 个位置参数,但给定了 3 个
我不明白为什么会出现这个错误以及如何修复它。请帮助我!
我是新手,尝试在网上寻找解决方案,但我还是相当新,没有找到与我的问题完全匹配的内容。
英文:
I have a large file that I'm trying to reduce using dataframe.drop
Here's my code:
probe_df = pd.read_csv(csv_file,header = 9233, nrows = 4608)
# Get rid of stuff
c_to_drop = 'Unnamed: ' + str(count_tp+1)
probe_df = probe_df.set_index("Chamber ID").drop(c_to_drop,1)
When I ran this, I got this error message:
probe_df = probe_df.set_index("Chamber ID").drop(c_to_drop,1)
TypeError: DataFrame.drop() takes from 1 to 2 positional arguments but 3 were given
I don't understand why I got the error and how to fix it. Please help!
I'm a newbie and I tried looking online for a solution but I'm still quite new and didn't see anything that matched my issue exactly.
答案1
得分: 2
根据pandas文档,drop
函数的源代码如下:
DataFrame.drop(labels=None, *, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')
*
代表了允许的位置参数的结束。这表明位置参数应该是labels
,而对于Python的错误消息,这可能来自于未直接显示的self
位置参数,因为它是隐式提供的。
因此,执行probe_df = probe_df.set_index("Chamber ID").drop(c_to_drop, 1)
将向一个只接受1个位置参数(不包括self
)的函数中传入2个位置参数。
通过将其更改为probe_df.set_index("Chamber ID").drop(c_to_drop, axis=1)
,我们将1
从位置参数转换为函数所需的关键字参数。
英文:
According to the pandas documentation, the source for drop would be
> DataFrame.drop(labels=None, *, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise')
The *
represents the end of allowed positional arguments. This indicates that the positional arguments would be labels
, and for python's error message this would likely be from the self
positional argument that is not directly shown as it is implicitly supplied.
Therefore, doing probe_df = probe_df.set_index("Chamber ID").drop(c_to_drop,1)
would be feeding 2 positional arguments into a function which only takes 1 (not including self
).
By changing it to probe_df.set_index("Chamber ID").drop(c_to_drop, axis=1)
, we convert the 1
from a positional argument to a keyword argument as required by the function.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论