1

I have a dataframe

+---+------------+-----------+
|   | wavelength | intensity |
+---+------------+-----------+
| 1 |        400 |        12 |
| 2 |        401 |        20 |
| 3 |        402 |        25 |
+---+------------+-----------+

and want to convert it to a dictionary

{400:12,
 401:20,
 402:25}

None of the orient parameters for the pandas.DataFrame.to_dict function gives me this output. How do I best get the desired output?

2 Answers 2

1
dict(zip(df['wavelength'],df['intensity']))
Sign up to request clarification or add additional context in comments.

Comments

1

Create Series with index by wavelength column and then use Series.to_dict:

d = df.set_index('wavelength')['intensity'].to_dict()

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.