在Python中重新排序Pandas DataFrame的列

时间:2020-02-23 14:42:09  来源:igfitidea点击:

在本教程中,我们会看到 3 different methods重新排序Pandas DataFrame的列:

使用Reindex方法

我们可以使用dataframe的 reindex()重新排序Pandas DataFrame列的方法。

我们需要将列= [$list_of_columns]传递给ReIndex()方法来重新排序Pandas DataFrame的列。

# import pandas package as pd in this code
import pandas as pd
 
# make a dictionary containing students data
data = {
'Name': ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
'Gender' : ['M','F','M','F'],
'Age': [23, 21, 22, 21],
'Course': ['B.tech', 'B.tech', 'B.sc', 'B.sc'],
'Branch': ['Cse','Cse','It','It'],
'College': ['Geu','Geu','Gehu','Gehu'],
}
 
# Convert the given dictionary into pandas DataFrame
df = pd.DataFrame(data, columns = ['Name','Gender', 'Age', 'Course','Branch', 'College'])
 
# print the pandas Dataframe
print("Given Dataframe :\n", df)
 
# reordering of the columns using reindex()
rslt_df = df.reindex(columns= ['Name', 'Age','Gender' ,'College','Course','Branch' ])
 
# print the pandas DataFrame
print("\nDataframe after Re-ordering of columns :\n", rslt_df)

输出 :

Given Dataframe :
Name Gender Age Course Branch College
0 Ankit M 23 B.tech Cse Geu
1 Aishwarya F 21 B.tech Cse Geu
2 Shaurya M 22 B.sc It Gehu
3 Shivangi F 21 B.sc It Gehu
Dataframe after Re-ordering of columns :
Name Age Gender College Course Branch
0 Ankit 23 M Geu B.tech Cse
1 Aishwarya 21 F Geu B.tech Cse
2 Shaurya 22 M Gehu B.sc It
3 Shivangi 21 F Gehu B.sc It

通过列名使用列选择

我们可以用 column selection through column namedataframe重新排序列。

这是一个例子:

# import pandas package as pd in this code
import pandas as pd
 
# make a dictionary containing students data
data = {
'Name': ['Ankit', 'Aishwarya', 'Ankita', 'Swapnil'],
'Gender' : ['M','F','F','M'],
'Age': [23, 21, 22, 21],
'Course': ['B.tech', 'B.tech', 'B.sc', 'B.sc'],
'Branch': ['Cse','Cse','It','It'],
'College': ['Geu','Geu','Gehu','Gehu'],
}
 
# Convert the given dictionary into pandas DataFrame
df = pd.DataFrame(data, columns = ['Name','Gender', 'Age', 'Course','Branch', 'College'])
 
# print the pandas Dataframe
print("Given Dataframe :\n", df)
 
# reordering of the columns by using column selection
rslt_df = df[['Name','Gender' ,'Age','College','Course','Branch' ]]
 
# print the pandas DataFrame
print("\nDataframe after Re-ordering of columns :\n", rslt_df)

输出 :

Given Dataframe :
Name Gender Age Course Branch College
0 Ankit M 23 B.tech Cse Geu
1 Aishwarya F 21 B.tech Cse Geu
2 Ankita F 22 B.sc It Gehu
3 Swapnil M 21 B.sc It Gehu
Dataframe after Re-ordering of columns :
Name Gender Age College Course Branch
0 Ankit M 23 Geu B.tech Cse
1 Aishwarya F 21 Geu B.tech Cse
2 Ankita F 22 Gehu B.sc It
3 Swapnil M 21 Gehu B.sc It

通过列索引使用列选择

我们可以用 column selection through column indexdataframe重新排序列。

这是一个例子:

# import pandas package as pd in this code
import pandas as pd
 
# make a dictionary containing students data
data = {
'Name': ['Ankit', 'Aishwarya', 'Ankita', 'Swapnil'],
'Gender' : ['M','F','F','M'],
'Age': [23, 21, 22, 21],
'Course': ['B.tech', 'B.tech', 'B.sc', 'B.sc'],
'Branch': ['Cse','Cse','It','It'],
'College': ['Geu','Geu','Gehu','Gehu'],
}
 
# Convert the given dictionary into pandas DataFrame
df = pd.DataFrame(data, columns = ['Name','Gender', 'Age', 'Course','Branch', 'College'])
 
# print the pandas Dataframe
print("Given Dataframe :\n", df)
 
# reordering of the columns by use of column index.
rslt_df = df[df.columns[[5,4,3,2,1,0]]]
 
# print the pandas DataFrame
print("\nDataframe after Re-ordering of columns :\n", rslt_df)

输出 :

Given Dataframe :
Name Gender Age Course Branch College
0 Ankit M 23 B.tech Cse Geu
1 Aishwarya F 21 B.tech Cse Geu
2 Ankita F 22 B.sc It Gehu
3 Swapnil M 21 B.sc It Gehu
Dataframe after Re-ordering of columns :
College Branch Course Age Gender Name
0 Geu Cse B.tech 23 M Ankit
1 Geu Cse B.tech 21 F Aishwarya
2 Gehu It B.sc 22 F Ankita
3 Gehu It B.sc 21 M Swapnil