Linked Questions
78 questions linked to/from How to change the order of DataFrame columns?
21
votes
1
answer
8k
views
Pandas Pivot Table manually sort columns [duplicate]
For a given data frame:
UUT testa testb testc testd
DateTime
2017-11-21 18:47:29 1.0 1.0 1.0 3.0
2017-11-21 18:47:30 1.0 2.0 1....
5
votes
1
answer
8k
views
How to move a column in a pandas dataframe [duplicate]
I want to take a column indexed 'length' and make it my second column. It currently exists as the 5th column. I have tried:
colnames = big_df.columns.tolist()
# make index "length" the second column ...
4
votes
1
answer
8k
views
Sort Pandas dataframe according to list of column names [duplicate]
I have a pandas dataframe like this-
d = {'class': [0, 1,1,0,1,0], 'A': [0,4,8,1,0,0],'B':[4,1,0,0,3,1],'Z':[0,9,3,1,4,7]}
df = pd.DataFrame(data=d)
A B Z class
0 0 4 0 0
1 4 1 ...
1
vote
1
answer
2k
views
Python Pandas: Split column and add new column next to current [duplicate]
I have a excel sheet similar to this, although with a lot more columns:
Team
Members
Team1 (553)
95435
Team2 (443)
872
I want to split the team column into Team and a new column, named Team ID. I ...
0
votes
1
answer
985
views
Reorder a dataframe from a dictionary with columns of different size using python [duplicate]
I'm trying to generate a dataframe using Pandas like this:
import pandas as pd
x='x'
y='y'
z='z'
Area='Area'
#len(coords_x)==len(coords_y)==len(coords_z)==64
#len(area[:,0])==18
my_dict = dict( x= ...
1
vote
1
answer
376
views
Move Column to Front of DataFrame in Python [duplicate]
I've looked around but can't find an exact solution, though it should be easily accomplished task:
I have a DataFrame and I just want to move one of the columns to the front. How do I do this ...
-1
votes
2
answers
80
views
Order columns in dataframe based on values from a set [duplicate]
I have a pandas df like this
id v1 v2 v3 v4
1 a a b b
2 x f f a
How can I order it with a based on values from a set such as
setorder = ('1','3','2','4')
yields
id v1 v3 v2 v4
1 a b a b
...
0
votes
0
answers
117
views
How to order pandas datafame column by given a list of column [duplicate]
For example, I have a pandas dataframe like this :
T2 T4 T3 T1 Time
1 0.1 0.7 0.6 0.2 18:00
2 0.3 0.4 0.1 0.4 16:00
3 0.2 0.3 0.2 0.9 20:00
4 0.1 0.7 0.5 0.4 19:00
5 0.1 0.4 0.8 0.9 17:...
0
votes
1
answer
105
views
Questions Tags Users Unanswered shifting specific column to before/after specific column in dataframe [duplicate]
In dataframe example :
medcine_preg_oth medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3
0 Berplex Berplex None None
1 NaN ...
0
votes
0
answers
82
views
How to change the order of columns in a pandas Dataframe [duplicate]
I have pivot table:
avg count
days 10d 1d 20d 3d 5d 10d 1d 20d 3d 5d
range
0.1 5.9 % 1.25 % 8.27 % 3.28 % 4.42 % 1104.0 1104.0 1104.0 ...
2
votes
0
answers
77
views
Ordering Columns in custom orders after unstacking [duplicate]
I currently have a dataframe that looks like this
CODE DATE VALUE
0001 2020-10-09 100
0001 2020-10-10 102
0002 2020-10-09 50
0002 2020-10-10 48
0003 2020-10-...
1
vote
0
answers
62
views
How to add unrepeated row of second csv to the end on first one and merge to new csv? [duplicate]
I have two csv file like test1.csv, test2.csv and in them some column headers aren't repeated and I need to append second unrepeated column to end of first and merge it to 'merge.csv' when my program ...
0
votes
1
answer
38
views
How to add columns to a dataframe in a specific order? [duplicate]
The issue I am having is that I want a ratio column after every variable, and not at the very end. say my dataframe is df =
A B C total
15 30 45 90
20 30 50 100
10 20 30 60
I want ...
0
votes
0
answers
36
views
Pandas: is there a method to order columns like in R? [duplicate]
R's tidyverse offers the function relocate() to move columns from in specific positions (e.g., df %>% relocate("col1", .after="col2"). Is there a way to do this in Python Pandas,...
1796
votes
22
answers
4.2m
views
Selecting multiple columns in a Pandas dataframe
How do I select columns a and b from df, and save them into a new dataframe df1?
index a b c
1 2 3 4
2 3 4 5
Unsuccessful attempt:
df1 = df['a':'b']
df1 = df.ix[:, 'a':'b']