question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: moving columns to first or last index of a dataframe

See original GitHub issue

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I wish I can use a function to move a column in the start or end of a DataFrame using moveToStart and moveToEnd functions.

Feature Description

df.moreToStart("target")
df.moveToEne("target")

Alternative Solutions

temp_cols=df.columns.tolist()
index=df.columns.get_loc("target")
new_cols= temp_cols[0:index] + temp_cols[index+1:] + temp_cols[index:index+1]
df=df[new_cols]

Additional Context

Moving target variables in the end to make dataset more smooth for machine learning algorithms.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rhshadrachcommented, Aug 1, 2022

-1 as well; I don’t think this is something we should be expanding the API for. Perhaps it’d be helpful to write your own utility function(s), something like:

def first_to_front(haystack, needle):
    idx = haystack.index(needle)
    result = [haystack[idx]] + haystack[:idx] + haystack[idx+1:]
    return result

def pd_first_to_front(df, needle):
    result = df[first_to_front(df.columns.tolist(), needle)]
    return result

df = pd.DataFrame({'a': [1], 'b': 2, 'c': 3, 'd': 4})
print(pd_first_to_front(df, 'c'))
1reaction
attack68commented, Aug 1, 2022

-1 on this, since it is just too specific task to have its own function, when methods already exist for simply display:

df = pd.DataFrame({"a": [1,2,3], "b": [4,5,6], "c": [6,7,8]})
df[["c", "a", "b"]]  # moves last to first
df[["c"] + [_ for _ in df.columns if _ != "c"]]  # same
df.reindex(["c", "a", "b"], axis=1)  # same

And any of the above are fully generalisable methods

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Move a Column to First Position in Pandas DataFrame?
The basic idea to move a column in a pandas dataframe is to remove the column from its current place and insert it...
Read more >
Pandas - How to Change Position of a Column
Moving first to last and last to first is simple, now let's see moving the middle column to the first position of the...
Read more >
Move column by name to front of table in pandas
Show activity on this post. My current code moves the column by index using df. columns. tolist() but I'd like to shift it...
Read more >
Move column to front in a Pandas DataFrame - EasyTweaks.com
1. Rearrange column to first position · 2. Move Pandas column to a specific position (in this case the second) · 3. Reorder...
Read more >
could not convert string to float sklearn standardscaler
Enhancement datasets. The output variable contains three different string values. All columns of the dataframe are float and the output y is also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found