pandas icon indicating copy to clipboard operation
pandas copied to clipboard

ENH: df.columns returns list of series

Open R3dan opened this issue 2 years ago • 5 comments

Feature Type

  • [X] Adding new functionality to pandas

  • [X] Changing existing functionality in pandas

  • [ ] Removing existing functionality in pandas

Problem Description

Currently you can go df[{column label}] to access a column or df.columns to access the column labels. I propose either a new list that is like df.columns but with the series returned by df[{column}] or just replace df.columns with this new column. As long as you can access the label I don't see why not. This could also allow for adding or removinf new columns by editing df.columns

Feature Description

Completely new code could be needed or accessing the same as getitem does:

[i for i in indexes] with indexes being the columns.

Alternative Solutions

for i in df: column=df[i]

Additional Context

No response

R3dan avatar Feb 13 '24 22:02 R3dan

Thanks for the request. I would recommend using [e for _, e in df.items()].

What advantage does adding a method here provide a user that is difficult / not possible today?

rhshadrach avatar Feb 13 '24 22:02 rhshadrach

This could also allow for adding or removinf new columns by editing df.columns

Adding a new method for getting a list of Series wouldn't be that difficult (though im -1 since there are easy alternatives), but making it editable like this would be difficult.

jbrockmendel avatar Feb 14 '24 18:02 jbrockmendel

I think the biggest problem is how DataFrame.values ended up being implemented. I'm slightly curious to know why it wasn't done to return an iterable of Series that follow the .keys and .items analog from dict

Delengowski avatar Feb 16 '24 19:02 Delengowski

I would hazard a guess that .values as added prior to any active core member. In any case, I think it would be far too disruptive to change now.

rhshadrach avatar Feb 16 '24 21:02 rhshadrach

Absolutely, I wasn't suggesting that. More being sympathetic to OP here.

Delengowski avatar Feb 17 '24 15:02 Delengowski

closing, -1 on doing this as well

phofl avatar Mar 18 '24 01:03 phofl