1

I have a dataframe that looks like

Total_Time_words    Words
0      1.50         your
1      2.15         intention
2      2.75         is
3      3.40         dangerous
4      3.85         for

when I use this code:

new.set_index('Words').T.to_dict('records')

I get this output below:

[{'your': 1.5,
  'intention': 2.15,
  'is': 2.75,
  'dangerous': 3.4,
  'for': 3.85,
  'my': 4.0,
  'world': 4.3}]

But this is my expected output below:

[
   {
      1.50:"your"
   },
   
   {
      2.15:"intention"
   }
]
2
  • Is it json format? Commented Jul 30, 2020 at 5:56
  • dataframe, i want to convert the dataframe to ordinary dictionary {'your': 1.5}, { 'intention': 2.15} Commented Jul 30, 2020 at 5:58

1 Answer 1

1

You can use list comprehension with zip as below:

new_dict = [{k:v} for k,v in zip(df["Total_Time_words"], df["words"])]
print(new_dict)
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.