Could you explain to me why the Properties
column was the third column and not the first one? As you can see I insert it as the first in pd.DataFrame
, but when I do print(df)
, it appears as the third column.
JavaScript
x
15
15
1
import pandas as pd
2
3
df = pd.DataFrame({'Properties':[1, 2, 3,4],
4
'Latitude':[-24.930473, -24.95575,-24.924161,-24.95579],
5
'Longitude':[-24.930473, -24.95575,-24.924161,-24.95579],
6
'cluster': (1,2,1,2)})
7
8
print(df)
9
10
Latitude Longitude Properties cluster
11
0 -24.930473 -24.930473 1 1
12
1 -24.955750 -24.955750 2 2
13
2 -24.924161 -24.924161 3 1
14
3 -24.955790 -24.955790 4 2
15
Advertisement
Answer
Try using columns
argument to assign the order of columns:
JavaScript
1
6
1
import pandas as pd
2
df = pd.DataFrame({'C1':[1, 2, 3,4],
3
'C2':[-24.930473, -24.95575,-24.924161,-24.95579],
4
'C3':[-24.930473, -24.95575,-24.924161,-24.95579],
5
'C4': (1,2,1,2)}, columns=['C1', 'C3', 'C2', 'C4'])
6
This gives:
JavaScript
1
6
1
C1 C3 C2 C4
2
0 1 -24.930473 -24.930473 1
3
1 2 -24.955750 -24.955750 2
4
2 3 -24.924161 -24.924161 1
5
3 4 -24.955790 -24.955790 2
6