How to print a dataframe which is read from a csv file into dictionary format?

Viewed 39

I turned the data in a csv file into a dataframe by using Pandas then proceeded to print the dataframe into dictionary format by using Ordereddict.

However, the index numbers for each row are missing when I did it, is that anyway to retain the index number or anyway to achieve the same result with index numbers?

And I am not entirely sure the part with zip() and tolist(), can anyone explain their usages?

My code:

import pandas as pd
from collections import OrderedDict
import collections

df = pd.read_csv('for_testing.csv')


for i, row in df.iterrows():
  d = OrderedDict(zip(row.index.tolist(), row.tolist()))
  print(d)

Output:

OrderedDict([('First Name', 'Kate'), ('Last Name', 'Rose'), ('Occupation', 'Teacher'), ('Age', 24), ('Number', 87)])
OrderedDict([('First Name', 'James'), ('Last Name', 'Smith'), ('Occupation', 'Cook'), ('Age', 35), ('Number', 487)])
OrderedDict([('First Name', 'Nick'), ('Last Name', 'Carter'), ('Occupation', 'Writer'), ('Age', 44), ('Number', 896)])
OrderedDict([('First Name', 'Ray'), ('Last Name', 'Johnson'), ('Occupation', 'Designer'), ('Age', 34), ('Number', 412)])
OrderedDict([('First Name', 'Jay'), ('Last Name', 'Law'), ('Occupation', 'Unemployed'), ('Age', 25), ('Number', 123)])

I would like the Output to be like this:

1. OrderedDict([('First Name', 'Kate'), ('Last Name', 'Rose'), ('Occupation', 'Teacher'), ('Age', 24), ('Number', 87)])
2. OrderedDict([('First Name', 'James'), ('Last Name', 'Smith'), ('Occupation', 'Cook'), ('Age', 35), ('Number', 487)])
3. OrderedDict([('First Name', 'Nick'), ('Last Name', 'Carter'), ('Occupation', 'Writer'), ('Age', 44), ('Number', 896)])
4. OrderedDict([('First Name', 'Ray'), ('Last Name', 'Johnson'), ('Occupation', 'Designer'), ('Age', 34), ('Number', 412)])
5. OrderedDict([('First Name', 'Jay'), ('Last Name', 'Law'), ('Occupation', 'Unemployed'), ('Age', 25), ('Number', 123)])

Edit: I noticed a problem, when I try to make the print triggered by an user input, it only displays the last row of the ordereddict dataframe instead of the whole ordereddict dataframe:

New code:

import pandas as pd
from collections import OrderedDict
import collections

df = pd.read_csv('for_testing.csv')

def main():
  choice = input("type sth ")
  
  if choice == '1':
      for i, row in df.iterrows():
        d = OrderedDict(zip(row.index.tolist(), row.tolist()))
      print(d)


main()

New Output:

type sth 1
OrderedDict([('First Name', 'Jay'), ('Last Name', 'Law'), ('Occupation', 'Unemployed'), ('Age', 25), ('Number', 123)])
1 Answers

You're overwriting your dictionary each time you define it in your loop. You can start by defining an empty dictionary outside of your loop, then assigning values to it in your loop. With OrderedDict, it should keep the items in the order you assign them:

d = OrderedDict()
for i, row in df.iterrows():
    d[i] = list(zip(row.index.tolist(), row.tolist()))
print(d)
Related