简体   繁体   English

如何根据新职位列表对列进行重新排序

[英]How to reorder columns according to a list of new positions

I have a pandas data frame of dimensions (7096, 94), each index has a name. 我有一个尺寸为(7096,94)的熊猫数据框,每个索引都有一个名称。 I want to reorder the columns with that list of positions/locations : 我想使用该位置/位置列表对列进行重新排序:

[92, 57, 73, 81, 62, 64, 18, 93, 63, 89, 56, 72, 78, 26, 28, 79, 40, 54, 55, 53, 27, 21, 70, 25,

51, 7, 36, 23, 15, 59, 71, 0, 5, 91, 39, 67, 90, 77, 86, 2, 61, 69, 82, 46, 47, 45, 85, 75, 83, 6, 88, 65, 34, 52, 4, 8, 29, 38, 35, 33, 60, 84, 80, 49, 24, 13, 3, 14, 12, 68, 16, 17, 41, 31, 10, 87, 32, 11, 20, 76, 43, 66, 48, 37, 44, 74, 42, 22, 58, 1, 19, 50, 30, 9] 51,7,36,23,15,59,71,0,5,91,39,67,90,77,86,2,61,69,82,46,47,45,85,75,83, 6,88,65,34,52,4,8,8,29,38,35,33,60,84,80,49,24,13,3,14,12,68,16,17,41,31, 10、87、32、11、20、76、43、66、48、37、44、74、42、22、58、1、19、50、30、9]

cols = [92, 57, 73, 81, 62, 64, 18, 93, 63, 89, 56, 72, 78, 26, 28, 79, 40, 54, 55, 53, 27, 21, 70, 25, 51, 7, 36, 23, 15, 59, 71, 0, 5, 91, 39, 67, 90, 77, 86, 2, 61, 69, 82, 46, 47, 45, 85, 75, 83, 6, 88, 65, 34, 52, 4, 8, 29, 38, 35, 33, 60, 84, 80, 49, 24, 13, 3, 14, 12, 68, 16, 17, 41, 31, 10, 87, 32, 11, 20, 76, 43, 66, 48, 37, 44, 74, 42, 22, 58, 1, 19, 50, 30, 9]

cols = [df.columns[c] for c in cols]
df = df[cols]

You can use pd.DataFrame.iloc to feed column indices directly: 您可以使用pd.DataFrame.iloc直接提供列索引:

cols = [92, 57, 73, 81, 62, 64, 18, ...]
df = df.iloc[:, cols]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM