shifting specific column to before/after specific column in dataframeCreating new columns by iterating over rows in pandas dataframeReplacing column values in PandasHow can I fill NaN values in a pandas data frame?Imputation of missing values and dealing with categorical valueshow many rows have values from the same columns pandasHow to use LSTM to make prediction with both feature from the past and the current ones?Resampling pandas Dataframe keeping other columnsPandas Conditional Fill NaN Forward/BackwardHow can I merge 2+ DataFrame objects without duplicating column names?Merging dataframes in Pandas is taking a surprisingly long time
Is it ethical to recieve stipend after publishing enough papers?
Doesn't the system of the Supreme Court oppose justice?
Microchip documentation does not label CAN buss pins on micro controller pinout diagram
PTIJ: Why is Haman obsessed with Bose?
What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?
US tourist/student visa
How to make money from a browser who sees 5 seconds into the future of any web page?
Is there a RAID 0 Equivalent for RAM?
Is this part of the description of the Archfey warlock's Misty Escape feature redundant?
Does the reader need to like the PoV character?
What do you call a word that can be spelled forward or backward forming two different words
Taxes on Dividends in a Roth IRA
How much theory knowledge is actually used while playing?
How would you translate "more" for use as an interface button?
Is this toilet slogan correct usage of the English language?
Isometries between spherical space forms
I found an audio circuit and I built it just fine, but I find it a bit too quiet. How do I amplify the output so that it is a bit louder?
When were female captains banned from Starfleet?
Review your own paper in Mathematics
Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?
Which Article Helped Get Rid of Technobabble in RPGs?
Why is so much work done on numerical verification of the Riemann Hypothesis?
How do I fix the group tension caused by my character stealing and possibly killing without provocation?
Non-trope happy ending?
shifting specific column to before/after specific column in dataframe
Creating new columns by iterating over rows in pandas dataframeReplacing column values in PandasHow can I fill NaN values in a pandas data frame?Imputation of missing values and dealing with categorical valueshow many rows have values from the same columns pandasHow to use LSTM to make prediction with both feature from the past and the current ones?Resampling pandas Dataframe keeping other columnsPandas Conditional Fill NaN Forward/BackwardHow can I merge 2+ DataFrame objects without duplicating column names?Merging dataframes in Pandas is taking a surprisingly long time
$begingroup$
In dataframe example :
medcine_preg_oth medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3
0 Berplex Berplex None None
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 obmin obmin None None
4 NaN NaN NaN NaN
'medcine_preg_oth1' 'medcine_preg_oth2' 'medcine_preg_oth3' ,these three columns are in somewhere of dataframe with other columns.
I want to shift these three : medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3 to the place of after 'medcine_preg_oth'.
My idea is shifting the specific columns to place after/ before specific columns in dataframe for wider purpose .
please suggest me!
Thanks
python pandas data-cleaning
New contributor
$endgroup$
add a comment |
$begingroup$
In dataframe example :
medcine_preg_oth medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3
0 Berplex Berplex None None
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 obmin obmin None None
4 NaN NaN NaN NaN
'medcine_preg_oth1' 'medcine_preg_oth2' 'medcine_preg_oth3' ,these three columns are in somewhere of dataframe with other columns.
I want to shift these three : medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3 to the place of after 'medcine_preg_oth'.
My idea is shifting the specific columns to place after/ before specific columns in dataframe for wider purpose .
please suggest me!
Thanks
python pandas data-cleaning
New contributor
$endgroup$
add a comment |
$begingroup$
In dataframe example :
medcine_preg_oth medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3
0 Berplex Berplex None None
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 obmin obmin None None
4 NaN NaN NaN NaN
'medcine_preg_oth1' 'medcine_preg_oth2' 'medcine_preg_oth3' ,these three columns are in somewhere of dataframe with other columns.
I want to shift these three : medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3 to the place of after 'medcine_preg_oth'.
My idea is shifting the specific columns to place after/ before specific columns in dataframe for wider purpose .
please suggest me!
Thanks
python pandas data-cleaning
New contributor
$endgroup$
In dataframe example :
medcine_preg_oth medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3
0 Berplex Berplex None None
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 obmin obmin None None
4 NaN NaN NaN NaN
'medcine_preg_oth1' 'medcine_preg_oth2' 'medcine_preg_oth3' ,these three columns are in somewhere of dataframe with other columns.
I want to shift these three : medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3 to the place of after 'medcine_preg_oth'.
My idea is shifting the specific columns to place after/ before specific columns in dataframe for wider purpose .
please suggest me!
Thanks
python pandas data-cleaning
python pandas data-cleaning
New contributor
New contributor
edited Mar 18 at 7:00
Toros91
1,9942628
1,9942628
New contributor
asked Mar 18 at 6:33
Theinzaw Theinzaw
1
1
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
If I get the question correct, you just need to change the order of your columns. This can be simply done by reassigning the new order of your columns to the dataframe.
For example:
#['a', 'b', 'c'] <-given columns order
df = df[['c', 'b', 'a']]
You can also use the built-in function reindex
to accomplish you task as follow:
cols = df.columns.tolist() #['a', 'b', 'c']
new_cols = [['c', 'b', 'a']]
df = df.reindex(columns=new_cols)
EDIT:
If you have a large number of columns, the problem will arise in how you get the new_cols
list. To do this you can use list indexing and slicing. Firstly get the index of columns you wnat to replace by using:
df.columns.get_loc("b") #1
Now suppose you have 699 columns and want to place the 100th and 200th column after the 7th one, you can do this:
cols = cols[0:7] + [cols[100]] + [cols[200]] + cols[8:100] + cols[101:200] + cols[201:]
You can now use this column to change the order of your columns in the above mentioned way. The expression will vary depending on your use case.
$endgroup$
$begingroup$
Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
$endgroup$
– Theinzaw
Mar 18 at 7:39
$begingroup$
I have edited the answer.
$endgroup$
– bkshi
Mar 18 at 11:40
add a comment |
$begingroup$
x=df.columns.get_loc('col') # getting column index , the position before that col.
var_list=new_var # list of columns that are wanted to move or shift if the column are
# multiples and also they are in sequence index column.
new_position = x
for var in var_list:
cols = df.columns.tolist()
column_to_move = var
new_position += 1
cols.insert(new_position, cols.pop(cols.index(column_to_move)))
df = df[cols]
New contributor
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "557"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Theinzaw is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47496%2fshifting-specific-column-to-before-after-specific-column-in-dataframe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
If I get the question correct, you just need to change the order of your columns. This can be simply done by reassigning the new order of your columns to the dataframe.
For example:
#['a', 'b', 'c'] <-given columns order
df = df[['c', 'b', 'a']]
You can also use the built-in function reindex
to accomplish you task as follow:
cols = df.columns.tolist() #['a', 'b', 'c']
new_cols = [['c', 'b', 'a']]
df = df.reindex(columns=new_cols)
EDIT:
If you have a large number of columns, the problem will arise in how you get the new_cols
list. To do this you can use list indexing and slicing. Firstly get the index of columns you wnat to replace by using:
df.columns.get_loc("b") #1
Now suppose you have 699 columns and want to place the 100th and 200th column after the 7th one, you can do this:
cols = cols[0:7] + [cols[100]] + [cols[200]] + cols[8:100] + cols[101:200] + cols[201:]
You can now use this column to change the order of your columns in the above mentioned way. The expression will vary depending on your use case.
$endgroup$
$begingroup$
Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
$endgroup$
– Theinzaw
Mar 18 at 7:39
$begingroup$
I have edited the answer.
$endgroup$
– bkshi
Mar 18 at 11:40
add a comment |
$begingroup$
If I get the question correct, you just need to change the order of your columns. This can be simply done by reassigning the new order of your columns to the dataframe.
For example:
#['a', 'b', 'c'] <-given columns order
df = df[['c', 'b', 'a']]
You can also use the built-in function reindex
to accomplish you task as follow:
cols = df.columns.tolist() #['a', 'b', 'c']
new_cols = [['c', 'b', 'a']]
df = df.reindex(columns=new_cols)
EDIT:
If you have a large number of columns, the problem will arise in how you get the new_cols
list. To do this you can use list indexing and slicing. Firstly get the index of columns you wnat to replace by using:
df.columns.get_loc("b") #1
Now suppose you have 699 columns and want to place the 100th and 200th column after the 7th one, you can do this:
cols = cols[0:7] + [cols[100]] + [cols[200]] + cols[8:100] + cols[101:200] + cols[201:]
You can now use this column to change the order of your columns in the above mentioned way. The expression will vary depending on your use case.
$endgroup$
$begingroup$
Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
$endgroup$
– Theinzaw
Mar 18 at 7:39
$begingroup$
I have edited the answer.
$endgroup$
– bkshi
Mar 18 at 11:40
add a comment |
$begingroup$
If I get the question correct, you just need to change the order of your columns. This can be simply done by reassigning the new order of your columns to the dataframe.
For example:
#['a', 'b', 'c'] <-given columns order
df = df[['c', 'b', 'a']]
You can also use the built-in function reindex
to accomplish you task as follow:
cols = df.columns.tolist() #['a', 'b', 'c']
new_cols = [['c', 'b', 'a']]
df = df.reindex(columns=new_cols)
EDIT:
If you have a large number of columns, the problem will arise in how you get the new_cols
list. To do this you can use list indexing and slicing. Firstly get the index of columns you wnat to replace by using:
df.columns.get_loc("b") #1
Now suppose you have 699 columns and want to place the 100th and 200th column after the 7th one, you can do this:
cols = cols[0:7] + [cols[100]] + [cols[200]] + cols[8:100] + cols[101:200] + cols[201:]
You can now use this column to change the order of your columns in the above mentioned way. The expression will vary depending on your use case.
$endgroup$
If I get the question correct, you just need to change the order of your columns. This can be simply done by reassigning the new order of your columns to the dataframe.
For example:
#['a', 'b', 'c'] <-given columns order
df = df[['c', 'b', 'a']]
You can also use the built-in function reindex
to accomplish you task as follow:
cols = df.columns.tolist() #['a', 'b', 'c']
new_cols = [['c', 'b', 'a']]
df = df.reindex(columns=new_cols)
EDIT:
If you have a large number of columns, the problem will arise in how you get the new_cols
list. To do this you can use list indexing and slicing. Firstly get the index of columns you wnat to replace by using:
df.columns.get_loc("b") #1
Now suppose you have 699 columns and want to place the 100th and 200th column after the 7th one, you can do this:
cols = cols[0:7] + [cols[100]] + [cols[200]] + cols[8:100] + cols[101:200] + cols[201:]
You can now use this column to change the order of your columns in the above mentioned way. The expression will vary depending on your use case.
edited Mar 18 at 12:20
answered Mar 18 at 7:15
bkshibkshi
638111
638111
$begingroup$
Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
$endgroup$
– Theinzaw
Mar 18 at 7:39
$begingroup$
I have edited the answer.
$endgroup$
– bkshi
Mar 18 at 11:40
add a comment |
$begingroup$
Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
$endgroup$
– Theinzaw
Mar 18 at 7:39
$begingroup$
I have edited the answer.
$endgroup$
– bkshi
Mar 18 at 11:40
$begingroup$
Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
$endgroup$
– Theinzaw
Mar 18 at 7:39
$begingroup$
Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
$endgroup$
– Theinzaw
Mar 18 at 7:39
$begingroup$
I have edited the answer.
$endgroup$
– bkshi
Mar 18 at 11:40
$begingroup$
I have edited the answer.
$endgroup$
– bkshi
Mar 18 at 11:40
add a comment |
$begingroup$
x=df.columns.get_loc('col') # getting column index , the position before that col.
var_list=new_var # list of columns that are wanted to move or shift if the column are
# multiples and also they are in sequence index column.
new_position = x
for var in var_list:
cols = df.columns.tolist()
column_to_move = var
new_position += 1
cols.insert(new_position, cols.pop(cols.index(column_to_move)))
df = df[cols]
New contributor
$endgroup$
add a comment |
$begingroup$
x=df.columns.get_loc('col') # getting column index , the position before that col.
var_list=new_var # list of columns that are wanted to move or shift if the column are
# multiples and also they are in sequence index column.
new_position = x
for var in var_list:
cols = df.columns.tolist()
column_to_move = var
new_position += 1
cols.insert(new_position, cols.pop(cols.index(column_to_move)))
df = df[cols]
New contributor
$endgroup$
add a comment |
$begingroup$
x=df.columns.get_loc('col') # getting column index , the position before that col.
var_list=new_var # list of columns that are wanted to move or shift if the column are
# multiples and also they are in sequence index column.
new_position = x
for var in var_list:
cols = df.columns.tolist()
column_to_move = var
new_position += 1
cols.insert(new_position, cols.pop(cols.index(column_to_move)))
df = df[cols]
New contributor
$endgroup$
x=df.columns.get_loc('col') # getting column index , the position before that col.
var_list=new_var # list of columns that are wanted to move or shift if the column are
# multiples and also they are in sequence index column.
new_position = x
for var in var_list:
cols = df.columns.tolist()
column_to_move = var
new_position += 1
cols.insert(new_position, cols.pop(cols.index(column_to_move)))
df = df[cols]
New contributor
New contributor
answered 2 days ago
Theinzaw Theinzaw
1
1
New contributor
New contributor
add a comment |
add a comment |
Theinzaw is a new contributor. Be nice, and check out our Code of Conduct.
Theinzaw is a new contributor. Be nice, and check out our Code of Conduct.
Theinzaw is a new contributor. Be nice, and check out our Code of Conduct.
Theinzaw is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47496%2fshifting-specific-column-to-before-after-specific-column-in-dataframe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown