-1

code:

import pandas as pd
data = pd.DataFrame({'Temp':[0,20,40,60],'Pressure':[0.0002,0.0012,0.0060,0.0300]})
print(data)

outcome:

   Pressure  Temp
0    0.0002     0
1    0.0012    20
2    0.0060    40
3    0.0300    60

However, I want this:

   Temp  Pressure
0   0    0.0002 
1  20    0.0012   
2  40    0.0060   
3  60    0.0300   

Why the order is not as I specified!

Version of pandas: pandas:0.22.0

Chris
  • 29,127
  • 3
  • 28
  • 51
Qiang
  • 1
  • 4
    It's not `pandas`, it's `dict`. `dict` does not keep the insertion order unless you use python 3.6 or higher. – Chris Feb 04 '20 at 01:46
  • 1
    Does this answer your question? [How to keep keys/values in same order as declared?](https://stackoverflow.com/questions/1867861/how-to-keep-keys-values-in-same-order-as-declared) – Chris Feb 04 '20 at 01:46
  • check with OrderedDict ~ – BENY Feb 04 '20 at 01:54

1 Answers1

0

That's because you are using python3.5 or lower, so dictionaries don't maintain insertion order, in your case use an OrderedDict like this:

import pandas as pd
from collections import OrderedDict

data = pd.DataFrame(OrderedDict({'Temp':[0,20,40,60],'Pressure':[0.0002,0.0012,0.0060,0.0300]}))
print(data)

>>>
   Temp  Pressure
0     0    0.0002
1    20    0.0012
2    40    0.0060
3    60    0.0300
marcos
  • 4,473
  • 1
  • 10
  • 24