Python 在数据帧创建时保持列顺序
我希望保持列的顺序与使用Python 在数据帧创建时保持列顺序,python,pandas,Python,Pandas,我希望保持列的顺序与使用pd.DataFrame定义的顺序一致。在下面的示例中,df.info显示GroupId是第一列,print也打印GroupId。 我使用的是Python版本3.6.3 import numpy as np import pandas as pd df = pd.DataFrame({'Id' : np.random.randint(1,100,10), 'GroupId' : np.random.randint(1,5,
pd.DataFrame
定义的顺序一致。在下面的示例中,df.info
显示GroupId是第一列,print
也打印GroupId。我使用的是Python版本3.6.3
import numpy as np
import pandas as pd
df = pd.DataFrame({'Id' : np.random.randint(1,100,10),
'GroupId' : np.random.randint(1,5,10) })
df.info()
print(df.iloc[:,0])
除非您使用python-3.6+对字典进行排序,否则(标准)字典不可能做到这一点。您需要将项目压缩到一起,并传递元组列表:
np.random.seed(0)
a = np.random.randint(1, 100, 10)
b = np.random.randint(1, 5, 10)
或者
一种方法是使用,如下所示。请注意,OrderedDict对象将元组列表作为输入
from collections import OrderedDict
df = pd.DataFrame(OrderedDict([('Id', np.random.randint(1,100,10)),
('GroupId', np.random.randint(1,5,10))]))
# Id GroupId
# 0 37 4
# 1 10 2
# 2 42 1
# 3 97 2
# 4 6 4
# 5 59 2
# 6 12 2
# 7 69 1
# 8 79 1
# 9 17 1
data = [a, b]
df = pd.DataFrame(list(zip(*data)), columns=['Id', 'GroupId']))
df
Id GroupId
0 45 3
1 48 1
2 65 1
3 68 1
4 68 3
5 10 2
6 84 3
7 22 4
8 37 4
9 88 3
from collections import OrderedDict
df = pd.DataFrame(OrderedDict([('Id', np.random.randint(1,100,10)),
('GroupId', np.random.randint(1,5,10))]))
# Id GroupId
# 0 37 4
# 1 10 2
# 2 42 1
# 3 97 2
# 4 6 4
# 5 59 2
# 6 12 2
# 7 69 1
# 8 79 1
# 9 17 1