Home > Blockchain >  Order in dataframe generation
Order in dataframe generation

Time:05-06

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.

import pandas as pd

df = pd.DataFrame({'Properties':[1, 2, 3,4],
                   'Latitude':[-24.930473, -24.95575,-24.924161,-24.95579],
                   'Longitude':[-24.930473, -24.95575,-24.924161,-24.95579],
                   'cluster': (1,2,1,2)})

print(df)

    Latitude  Longitude  Properties  cluster
0 -24.930473 -24.930473           1        1
1 -24.955750 -24.955750           2        2
2 -24.924161 -24.924161           3        1
3 -24.955790 -24.955790           4        2

enter image description here

CodePudding user response:

Try using columns argument to assign the order of columns:

import pandas as pd
df = pd.DataFrame({'C1':[1, 2, 3,4],
                   'C2':[-24.930473, -24.95575,-24.924161,-24.95579],
                   'C3':[-24.930473, -24.95575,-24.924161,-24.95579],
                   'C4': (1,2,1,2)}, columns=['C1', 'C3', 'C2', 'C4'])

This gives:

   C1         C3         C2  C4
0   1 -24.930473 -24.930473   1
1   2 -24.955750 -24.955750   2
2   3 -24.924161 -24.924161   1
3   4 -24.955790 -24.955790   2
  • Related