0

I have a dataframe as given below

id  DateTime              Name  Status
1   02112021 02:15:00   A   FALSE
1   02112021 02:15:00   B   TRUE
2   03112021 03:00:00   A   TRUE
2   03112021 03:00:00   B   FALSE
3   03112021 03:15:00   B   FALSE

I want to create a dictionary as given below:

dict = {'B' : { '02112021 02:15:00' : True, '03112021 03:00:00': FALSE, '03112021 03:15:00': FALSE}}

Here, B is the key came from the column Name and Keys inside the dictionary B came from the column DateTime and its values came from Status. It will take the datetime values as key and Status values as dictionary values that corresponds to B Can anyone please help, I am stucked on it.

2
  • Please fix your dictionary, this is not valid python Commented Nov 16, 2021 at 8:43
  • So need remove rows with Name=A ? Commented Nov 16, 2021 at 8:48

1 Answer 1

1

If need nested dictionary by Name column per groups use:

d = df.groupby('Name')[['DateTime','Status']].apply(lambda x: dict(x.to_numpy())).to_dict()
print (d)
{'A': {'02112021 02:15:00': False, '03112021 03:00:00': True},
 'B': {'02112021 02:15:00': True, '03112021 03:00:00': False, '03112021 03:15:00': False}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.