欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python dataframe修改列的顺序

程序员文章站 2022-07-12 14:11:50
...

首先创建一个dataframe,如下:

import pandas as pd
a=["1001","1002","1003"]
b=["xiaoming","xiaohong","xiaohua"]
c=["77","88","99"]
d=["87","82","91"]
dataframe = pd.DataFrame({'number':a,'name':b,'Python':c,'Math':d})

结果显示为:

  Math Python name number
0 87 77 xiaoming 1001
1 82 88 xiaohong 1002
2 91 99 xiaohua 1003

现在调整列的顺序:

order = ['number','name','Python','Math']
dataframe = dataframe[order]

结果显示为:

  number name Python Math
0 1001 xiaoming 77 87
1 1002 xiaohong 88 82
2 1003 xiaohua 99 91