Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reordering columns #28

Open
dlasusa opened this issue Nov 29, 2022 · 0 comments
Open

reordering columns #28

dlasusa opened this issue Nov 29, 2022 · 0 comments

Comments

@dlasusa
Copy link

dlasusa commented Nov 29, 2022

Hi!

So I'm using the "recipe style" of working on a dataframe and assigning some new new columns as part of that process (which works great).

Once of the last steps I'd like to do is put all the columns in a specific order. In this case, by "all" I mean some of the original columns as well as some of the newly created columns.

I understand (or at least think I understand) that since I want access to the new columns, which are in the intermediate df, I'll need to use a lambda.

Looking through Effective Pandas (p.229) Matt does a column rename:

.rename(columns=lambda c: c.replace('.', '_')

But this is doing the same thing to all the columns so I couldn't figure out how to apply this concept to a simple reorder. If I was doing this outside of the recipe, I can simply do:

df[cols in my order] # cols include old and new columns

But using the following inside the function/recipe

[cols in my order] # cols include old and new columns

Naturally fails since the new cols don't exist here.

It's not a huge deal to simply do the ordering after the recipe function is called, just wondering if it's something I can do as part of the recipe?

Thanks!
Dan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant