0

I have following DataFrame parsed into Python:

df = pd.read_csv("my_file.csv")

result:

   indexes X_values Y_values
0       IDX1     x1      y1
1       IDX1     x2      y2
2       IDX1     x3      y3
3       IDX1     x4      y4
6       IDX2     x1      y1
9       IDX2     x4      y4
10      IDX3     x1      y1
11      IDX3     x2      y2

I need to create dictionaries with each indexes as key and x_values & y_values as list of nested dictionaries. Output should be like:

{"IDX1" : [{"x1": "y1"}, {"x2": "y2"}, {"x3": "y3"}, {"x4": "y4"}],
"IDX2": [{"x1": "y1"},{"x4": "y4"}],
"IDX3":[{"x1": "y1"}, {"x2": "y2"}]}

I am trying to parse it using set_index() method but always missing something. Could you help me?

Also, dictionary of nested dictionaries with indexes as keys would be also good solution.

3 Answers 3

1

You can try

out = (df.apply(lambda row: {row['X_values']: row['Y_values']}, axis=1)
       .groupby(df['indexes']).agg(list).to_dict())
print(out)

{'IDX1': [{'x1': 'y1'}, {'x2': 'y2'}, {'x3': 'y3'}, {'x4': 'y4'}], 'IDX2': [{'x1': 'y1'}, {'x4': 'y4'}], 'IDX3': [{'x1': 'y1'}, {'x2': 'y2'}]}
Sign up to request clarification or add additional context in comments.

Comments

1

We can do

d = df[['X_values','Y_values']].apply(lambda x : {x[0]:x[1]},axis=1).groupby(df['indexes']).agg(list).to_dict()
Out[104]: 
{'IDX1': [{'x1': 'y1'}, {'x2': 'y2'}, {'x3': 'y3'}, {'x4': 'y4'}],
 'IDX2': [{'x1': 'y1'}, {'x4': 'y4'}],
 'IDX3': [{'x1': 'y1'}, {'x2': 'y2'}]}

Comments

0

Sounds like you need to make use of the to_dict() method (documentation).

You might need to index like you say. I would try:

df.set_index('indexes').T.to_dict('list')

1 Comment

This only returns one pair of x_values & y_values for each indexes.

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.