0

I have very basic knowledge of python and fairly new to pandas, This is my problem description and dataset in csv

enter image description here

I need to turn these two columns into a dictionary with keys having multiple values, and I have to show minimum a maximum values without using built in functions.

So, the output should be something like {Node_a : [64-4], Node_b : [87-4], Node_c : [84-5], Node_d : [54]}, and yes the format of the keys are also changed.

I have reached up to this output: {a: [12,23,64,4], b: [48,87,54,4], c: [5,84], d: [54]} by writing this code.

(data.groupby("node")["value"].apply(list).to_dict())

I am having a tough time retrieving each list of values finding min-max without the function and then further changing the values of the dictionary.

My approach may be long, I don't know, but is there a better way of doing it?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ben
  • 39
  • 6
  • `df.groupby('node')['value'].agg(list).to_dict()` or `df.groupby('node')['value'].agg(['min', 'max'])` – mozway Apr 15 '22 at 12:05
  • I've looked at the post, The questions are different, in this question we cannot use the min-max built in function and in the cat-dog question the min-max function is used, I wouldn't have asked the question if I was allowed to use built in functions. One has to retrieve the key values separately and then apply the code which find min-max values without using min(), max(). – Ben Apr 15 '22 at 12:26
  • so I guess this is an assignment? Then you should provide your code, explain how it eventually fail and request a specific help but not expect that someones does your homework ;) – mozway Apr 15 '22 at 12:29
  • I'm a fresher and a trainee here, It's a real-time data of the company so, I can't share the data because of it. Call it a job, assignment, task, whatever you like, our project manager is not always around so I can explain him how the code failed exactly, he doesn't have time for this, he just gives specific instructions, we need to follow through with the challenge, we can take any help to finish the daily task, what's wrong with that? – Ben Apr 15 '22 at 13:09
  • It's not wrong to ask for help, but if in the context of a company, you should use the pandas API (i.e. min/max). If this is to train you, then read [how to ask homework questions](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions), you should at least provide your code – mozway Apr 15 '22 at 13:13
  • I understand that, but if it's a challenge, **you** should solve it, not fellow SO users, you don't think so? ;) – mozway Apr 15 '22 at 13:32
  • It's a challenge, so asking why we can't use min, max is out of the question and when it comes to providing the code, if I skip the usual import pandas and reading the csv file it's just 1 line of code, here you go, (data.groupby("node")["value"].apply(list).to_dict()) - I can create the dictionary just like this, but the rest of it is tricky. – Ben Apr 15 '22 at 13:52
  • I get your point but we are allowed to ask for help, I have already spent a good chunk of my time fiddling around. I accidently posted incomplete comment so just now posted a new one. – Ben Apr 15 '22 at 13:55
  • Well, how would you manually find the max in a list `x`? Write the code, update your question. – mozway Apr 15 '22 at 13:56
  • I will update my question and add this piece of code and I know the usual method where we take a list, loop it, get the elements, compare them and then print it, but I can not even retrieve the list from the dictionary, this I have mentioned in the post, please read it carefully, so I had to ask for help, once I get the individual list then and only then I can perform the loop. – Ben Apr 15 '22 at 14:02

0 Answers0