Reputation: 123
I have a pandas data frame of dimensions (7096, 94), each index has a name. 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]
Upvotes: 0
Views: 1288
Reputation: 164773
You can use pd.DataFrame.iloc
to feed column indices directly:
cols = [92, 57, 73, 81, 62, 64, 18, ...]
df = df.iloc[:, cols]
Upvotes: 1
Reputation: 1661
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]
Upvotes: 2