How to change the order of Panas DataFrame columns
We will show how to use insert
and reindex
to change the order of columns in different ways pandas.DataFrame
, such as assigning column names in a desired order.
pandas.DataFrame
Sort the columns in the new order
The easiest way is columns
to reassign with a list of DataFrame
, or just assign the column names in the order we want:
# python 3.x
import pandas as pd
df = pd.DataFrame(
{
"a": ["1", "2", "3", "4"],
"b": [16, 7, 6, 16],
"c": [61, 57, 16, 36],
"d": ["12", "22", "13", "44"],
"e": ["Green", "Red", "Blue", "Yellow"],
"f": [1, 11, 23, 66],
}
)
print(df)
df = df[["e", "c", "b", "f", "d", "a"]]
print("Rearranging ..................")
print("..............................")
print(df)
Output:
a b c d e f
0 1 16 61 12 Green 1
1 2 7 57 22 Red 11
2 3 6 16 13 Blue 23
3 4 16 36 44 Yellow 66
Rearranging ..................
..............................
e c b f d a
0 Green 61 16 1 12 1
1 Red 57 7 11 22 2
2 Blue 16 6 23 13 3
3 Yellow 36 16 66 44 4
insert
Adding new columns and positions in Pandas using
If we want to create a new column, we can insert it wherever we want:
# python 3.x
import pandas as pd
df = pd.DataFrame(
{
"a": ["1", "2", "3", "4"],
"b": [16, 7, 6, 16],
"c": [61, 57, 16, 36],
"d": ["12", "22", "13", "44"],
"e": ["Green", "Red", "Blue", "Yellow"],
"f": [1, 11, 23, 66],
}
)
print(df)
print("Inserting ..................")
print("..............................")
df.insert(0, "newColMean", df.mean(1))
print(df)
Output:
newColMean a b c d e f
0 26.000000 1 16 61 12 Green 1
1 25.000000 2 7 57 22 Red 11
2 15.000000 3 6 16 13 Blue 23
3 39.333333 4 16 36 44 Yellow 66
reindex
Columns in a given order in Pandas
reindex
Arguably the most efficient way to rearrange columns:
# python 3.x
import pandas as pd
df = pd.DataFrame(
{
"a": ["1", "2", "3", "4"],
"b": [16, 7, 6, 16],
"c": [61, 57, 16, 36],
"d": ["12", "22", "13", "44"],
"e": ["Green", "Red", "Blue", "Yellow"],
"f": [1, 11, 23, 66],
}
)
print(df)
print("Rearranging ..................")
print("..............................")
df = df.reindex(columns=["a", "f", "d", "b", "c", "e"])
print(df)
Output:
a b c d e f
0 1 16 61 12 Green 1
1 2 7 57 22 Red 11
2 3 6 16 13 Blue 23
3 4 16 36 44 Yellow 66
Rearranging ..................
..............................
a f d b c e
0 1 1 12 16 61 Green
1 2 11 22 7 57 Red
2 3 23 13 6 16 Blue
3 4 66 44 16 36 Yellow
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to Convert DataFrame Column to String in Pandas
Publish Date:2025/05/02 Views:161 Category:Python
-
We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a
How to count the frequency of values in a Pandas DataFrame
Publish Date:2025/05/02 Views:84 Category:Python
-
Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().
How to get value from Pandas DataFrame cell
Publish Date:2025/05/02 Views:147 Category:Python
-
We'll look at using to get values from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .
How to Add a Row to a Pandas DataFrame
Publish Date:2025/05/02 Views:127 Category:Python
-
Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to
How to set values for specific cells in a Pandas DataFrame using index
Publish Date:2025/05/02 Views:118 Category:Python
-
Pandas is a data-centric python package that makes data analysis in python easy and consistent. In this article, we will look at different ways to access and set specific cell values in a pandas DataFrame data structure using indexing
Convert Pandas to CSV without index
Publish Date:2025/05/01 Views:159 Category:Python
-
As you know, an index can be thought of as a reference point used to store and access records in a DataFrame. They are unique for each row and usually range from 0 to the last row of the DataFrame, but we can also have serial numbers, dates
Convert Pandas DataFrame to Dictionary
Publish Date:2025/05/01 Views:198 Category:Python
-
This tutorial will show you how to convert a Pandas DataFrame into a dictionary with the index column elements as keys and the corresponding elements of other columns as values. We will use the following DataFrame in the article. import pan
Convert Pandas DataFrame columns to lists
Publish Date:2025/05/01 Views:192 Category:Python
-
When working with Pandas DataFrames in Python, you often need to convert the columns of the DataFrame into Python lists. This process is very important for various data manipulation and analysis tasks. Fortunately, Pandas provides several m
Subtracting Two Columns in Pandas DataFrame
Publish Date:2025/05/01 Views:120 Category:Python
-
Pandas can handle very large data sets and has a variety of functions and operations that can be applied to the data. One of the simple operations is to subtract two columns and store the result in a new column, which we will discuss in thi