Skip to content

Predictable order for columns in assign through sorting #9777

Closed
@mrocklin

Description

@mrocklin

The order of columns coming out of .assign is hard to predict

In [1]: import pandas as pd

In [2]: df = pd.DataFrame({'a': [1], 'b': [2]})

In [3]: df
Out[3]: 
   a  b
0  1  2

In [4]: df.assign(d=df.a*10, a=df.b*10, e=df.a*20)
Out[4]: 
    a  b   e   d
0  20  2  20  10

Would it be reasonable to keep all existing keys in their current location and then add the remaining keys in sorted order at the end?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions