0

I am having 1 dataframe "csv" as below

movieId |               title|              genres|
+-------+--------------------+--------------------+
|      1|    Toy Story (1995)|Adventure|Animati...|
|      2|      Jumanji (1995)|Adventure|Childre...|

COMMAND1 : print("PRINT",csvData.dtypes)
OUTPUT1 : ('PRINT', [('movieId', 'int'), ('title', 'string'), ('genres', 'string')])

COMMAND2 : print ("SAMPLE dictionary**",dict(csvData.dtypes))

OUTPUT2 :'SAMPLE dictionary**', {'genres': 'string', 'movieId': 'int', 'title': 'string'}

after comparing both OUTPUT we can see that dict() function changes the order of elements , how can i avoid that?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Punit
  • 23
  • 1
  • 4
  • 1
    `dict` does not guarantee order (or at least it didn't use to). `collections.OrderedDict` does. – Amadan Dec 26 '19 at 06:49
  • Python dictionaries are unordered. If you would like an OrderedDict, I suggest that you look at the following: https://pymotw.com/2/collections/ordereddict.html – Julian Chan Dec 26 '19 at 06:49
  • 4
    To be clear, this is true for Python versions 3.5 and earlier. Dictionaries retain insertion order from 3.6 onward. – navneethc Dec 26 '19 at 06:51
  • Does this answer your question? [Are dictionaries ordered in Python 3.6+?](https://stackoverflow.com/questions/39980323/are-dictionaries-ordered-in-python-3-6) – buran Dec 26 '19 at 06:54

0 Answers0