TypeError: DataFrame.drop() takes from 1 to 2 positional arguments but 3 were given
Asked Answered
D

2

8

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.

Dominik answered 6/6, 2023 at 2:21 Comment(0)
T
15

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.

Taverner answered 6/6, 2023 at 2:37 Comment(0)
C
0

If you want to drop column(s), you'll need to specify that using a keyword argument anyway (such as via axis=1 as in Shorn's answer), so instead of passing two arguments (labels and axis), you can directly pass the columns to drop to the columns= kwarg:

df = df.drop(columns=c_to_drop)            # <--- pass the labels to `columns=`

df.drop(columns=c_to_drop, inplace=True)   # <--- can also pass in-place
Chitin answered 20/8 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.