0

So I am new to python and I'm trying to do a small project which I want to do in pure pythonic way not using any additional libraries, where my data set looks like this:

LOC,DATE,ATTRIBUTE,COUNT
A,03/01/19,alpha,6483
A,03/01/19,beta,19
B,03/01/19,gamma,346158
B,02/01/19,gamma,156891
A,02/01/19,delta,1319
A,02/01/19,gamma,15272
A,02/01/19,gamma,56810

I have to transform this data set to this output:

B,02/01/19,gamma, 346158
A,02/01/19,alpha,6483
A,02/01/19,beta,19
B,02/01/19,gamma, 172163
A,02/01/19,delta,1319
B,01/01/19,gamma,56810

The data needs to be sorted by Date, Value, Measure, Loc

I thought that nested dictionaries should work, because I only have to update value of attirbute, LOC can be come the outer key

dict = {A:{}, B:{}}

Then date can be used as the key for the nested dictionary:

dict = {A:{03/01/19:{}, 02/01/19:{}}, B:{03/01/19:{}, 02/01/19:{}}

And keep going forward until I reach Count, and every time I keep updating count. But the code is getting more complex every time, my question:

  1. Is there any other alternative data structure that I could use?
  2. If with a dictionary is there a way to check nested keys and keep adding only new values for every key!

Any help would be really grateful!

wjandrea
  • 28,235
  • 9
  • 60
  • 81
afternoon
  • 67
  • 1
  • 4
  • 1
    You can consider using a `pandas.DataFrame` which you can then [`sort`](https://pandas.pydata.org/pandas-docs/version/0.19/generated/pandas.DataFrame.sort.html) however you'd like. Example: https://stackoverflow.com/questions/37787698/how-to-sort-pandas-dataframe-from-one-column – Cory Kramer Feb 18 '20 at 17:51
  • Hi, thanks for the reply. However, as I said, I want to do this pure pythonic way without using any libraries! – afternoon Feb 18 '20 at 18:25

0 Answers0