Reordering columns in CSV

2024/10/6 9:45:06

Question has been posted before but the requirements were not properly conveyed. I have a csv file with more than 1000 columns:

A   B   C   D ....  X   Y   Z
1   0   0.5 5 ....  1   7   6
2   0   0.6 4 ....  0   7   6
3   0   0.7 3 ....  1   7   6
4   0   0.8 2 ....  0   7   6

Here X , Y and Z are the 999, 1000, 10001 column and A, B, C , D are the 1st,2nd,3rd and 4th. I need to reorder the columns in such a way that it gives me the following.

D   Y   Z   A   B   C   ....X
5   7   6   1   0   0.5 ....1
4   7   6   2   0   0.6 ....0
3   7   6   3   0   0.7 ....1
2   7   6   4   0   0.8 ....0

that is 4th column becomes the 1st, 1000 and 1001th column becomes 2nd and 3rd and the other columns are shifted right accordingly.

Answer

So the question is how to reorder your columns in a custom way.

For example you have the following DF and you want to reorder your columns in the following way (indices):

5, 3, rest...

DF

In [82]: df
Out[82]:A  B    C  D  E  F  G
0  1  0  0.5  5  1  7  6
1  2  0  0.6  4  0  7  6
2  3  0  0.7  3  1  7  6
3  4  0  0.8  2  0  7  6

columns

In [83]: cols = df.columns.tolist()In [84]: cols
Out[84]: ['A', 'B', 'C', 'D', 'E', 'F', 'G']

reordered:

In [88]: cols = [cols.pop(5)] + [cols.pop(3)] + colsIn [89]: cols
Out[89]: ['F', 'D', 'A', 'B', 'C', 'E', 'G']In [90]: df[cols]
Out[90]:F  D  A  B    C  E  G
0  7  5  1  0  0.5  1  6
1  7  4  2  0  0.6  0  6
2  7  3  3  0  0.7  1  6
3  7  2  4  0  0.8  0  6
https://en.xdnf.cn/q/119792.html

Related Q&A

Variable not defined in while loop in python?

I am trying to write a simple program in python to read command line arguments and print a final word based on the arguments. If there is any argument of the form "-f=" then the will go to t…

Hours and time converting to a certain format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

Python socket server: listening to multiple clients [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

I have a problem with encoding with russian language for my python script [duplicate]

This question already has answers here:UnicodeEncodeError: ascii codec cant encode character u\xa0 in position 20: ordinal not in range(128)(34 answers)Closed last year.I am trying to send an email fro…

how do you style data frame in Pandas

I have this data frame: dfServer Env. Model Percent_Utilized server123 Prod Cisco. 50 server567. Prod Cisco. 80 serverabc. Prod IBM. 100 serverdwc.…

Vacation price program Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Why did push of a Flask app to Heroku failed?

Im simply trying to push my Flask app to Heroku but I encountered the following error: remote: ERROR: Command errored out with exit status 1: remote: command: /app/.heroku/python…

How to navigate through HTMl pages that have paging for their content using Python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…

How to merge one list elements with another list elements in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

Display and play audio files

I am new to python, and Im trying to build a simple recording program. With the some help from my previous question, I was able to add a timestamp for each recorded fileEDIT:I did some research and dec…