-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Closed
Labels
Description
Problem description
I'd like to suggest a modification to df.pop(item). Currently, pop(item)
deletes the column from the dataframe it's being called on and returns that column as a series. It doesn't accept multiple items.
It might be a nice convenience to:
- pop multiple columns at once (ex:
pop(['A', 'B'])
- specifying an
axis
parameter (default: axis=1) to allow popping rows and columns (ex:pop(1, axis=0)
) - pop slices (ex:
pop([1:3], axis=1)
)
Thought I'd throw it out there to the pandas gods and see if it is interesting. If it's not the best API design decision for pop
, I completely understand.
Common use-case
- you have one or multiple problem rows you want to delete from a dataframe but still keep for later evaluation. You'd just pop the rows and they'd be deleted from your existing dataframe and saved to a new variable.
- many times people seem to need to pop the last row, or second row. It is easy to pop the last row using
.iloc[:-1]
but popping the second row in one swoop isn't as easy I think. It could be if you just pop it out of there using pop. - sometimes people loop through a dataframe. not recommended I understand, but in such a scenario, you could pop a row based on a condition while looping perhaps in a complex manner.
Code Sample, a copy-pastable example if possible
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]},
columns=['A', 'B', 'C'])
def pop(df, values, axis=1):
if axis == 0:
if isinstance(values, (list, tuple)):
popped_rows = df.loc[values]
df.drop(values, axis=0, inplace=True)
return popped_rows
elif isinstance(values, (int)):
popped_row = df.loc[values].to_frame().T
df.drop(values, axis=0, inplace=True)
return popped_row
else:
print('values parameter needs to be a list, tuple or int.')
elif axis == 1:
# current df.pop(values) logic here
return df.pop(values)
Example Usage
# example df
>>> df
A B C
0 1 4 7
1 2 5 8
2 3 6 9
# pop multiple indices, delete from df inplace, return popped rows
# the df param wouldn't exist in the pop method; it'd be self
# df param just shown here to illustrate the idea
>>>pop(df, [0, 2], axis=0)
A B C
0 1 4 7
2 3 6 9
# pop one index value, delete from df, return row as a dataframe (not series)
>>> pop(df, 1, axis=0)
A B C
1 2 5 8
Demand for such a feature
tigerhawkvok, bpsuntrup, Smol-Cloud, pmav99, MaxPowerWasTaken and 25 more