0

from this datframe

  Total_Time_words  Words
0   1.50           Denisa
1   2.20             of
2   2.85           information
3   3.55            was
4   3.90            in

i use this to convert to dataframe:

new.T.to_dict()

i have a dictionary that looks like this:

{0: {'Total_Time_words': 1.5, 'Words': 'Denisa'},
 1: {'Total_Time_words': 2.2, 'Words': 'of'},
 2: {'Total_Time_words': 2.85, 'Words': 'information'},
 3: {'Total_Time_words': 3.55, 'Words': 'was'},
 4: {'Total_Time_words': 3.9, 'Words': 'in'},
 5: {'Total_Time_words': 4.05, 'Words': 'the'},
 6: {'Total_Time_words': 4.5, 'Words': 'University'}}

i want to remove all those number from 0 to 6 whixh are index position from the daraframe

expected output

{ {'Total_Time_words': 1.5, 'Words': 'Denisa'},
  {'Total_Time_words': 2.2, 'Words': 'of'},
  {'Total_Time_words': 2.85, 'Words': 'information'},
  {'Total_Time_words': 3.55, 'Words': 'was'},
  {'Total_Time_words': 3.9, 'Words': 'in'},
  {'Total_Time_words': 4.05, 'Words': 'the'},
  {'Total_Time_words': 4.5, 'Words': 'University'}}
nath
  • 113
  • 2
  • 11
  • 1
    That's meaningless. Probably you want a list of dictionaries? You can't have a dictionary with only values and no keys. Please edit your question and fix that mistake :) – Riccardo Bucco Jul 30 '20 at 10:02
  • 1
    your expected output is not a valid python object, because it a `set` of `dict`s, and `dict` is unhashable. – Adam.Er8 Jul 30 '20 at 10:02
  • Does this answer your question? [Pandas DataFrame to List of Dictionaries](https://stackoverflow.com/questions/29815129/pandas-dataframe-to-list-of-dictionaries) – Riccardo Bucco Jul 30 '20 at 10:13

3 Answers3

0
{ {'Total_Time_words': 1.5, 'Words': 'Denisa'},
  {'Total_Time_words': 2.2, 'Words': 'of'},
  {'Total_Time_words': 2.85, 'Words': 'information'},
  {'Total_Time_words': 3.55, 'Words': 'was'},
  {'Total_Time_words': 3.9, 'Words': 'in'},
  {'Total_Time_words': 4.05, 'Words': 'the'},
  {'Total_Time_words': 4.5, 'Words': 'University'}}

This is an invalid data structure. If you want to achieve what you're asking, you should store all the dictionaries in a list like this:

[{'Total_Time_words': 1.5, 'Words': 'Denisa'},
  {'Total_Time_words': 2.2, 'Words': 'of'},
  {'Total_Time_words': 2.85, 'Words': 'information'},
  {'Total_Time_words': 3.55, 'Words': 'was'},
  {'Total_Time_words': 3.9, 'Words': 'in'},
  {'Total_Time_words': 4.05, 'Words': 'the'},
  {'Total_Time_words': 4.5, 'Words': 'University'}]
0

This is not proper output:

{ {'Total_Time_words': 1.5, 'Words': 'Denisa'},
  {'Total_Time_words': 2.2, 'Words': 'of'},
  {'Total_Time_words': 2.85, 'Words': 'information'},
  {'Total_Time_words': 3.55, 'Words': 'was'},
  {'Total_Time_words': 3.9, 'Words': 'in'},
  {'Total_Time_words': 4.05, 'Words': 'the'},
  {'Total_Time_words': 4.5, 'Words': 'University'}}

Probably you mean that:

[ {'Total_Time_words': 1.5, 'Words': 'Denisa'},
  {'Total_Time_words': 2.2, 'Words': 'of'},
  {'Total_Time_words': 2.85, 'Words': 'information'},
  {'Total_Time_words': 3.55, 'Words': 'was'},
  {'Total_Time_words': 3.9, 'Words': 'in'},
  {'Total_Time_words': 4.05, 'Words': 'the'},
  {'Total_Time_words': 4.5, 'Words': 'University'} ]

What is the difference? In our second case, we have list of dicts. The problem with first case is, that dict (designated by "{" and "}") has key and value - and we don't have it there.

How to get what you want:

wanted_output = [v for k, v in your_dict.items()]
Dawid Gacek
  • 544
  • 3
  • 19
0

Say your dict is data. At the moment you can do something like data[4] and get

{'Total_Time_words': 3.9, 'Words': 'in'}

You could create a list instead, and get exactly the same behaviour:

min_key = min(data.keys())
max_key = max(data.keys())

if max_key - min_key + 1 != len(data.keys()) or min_key != 0:
    print("It is not possible to convert the dict into a list")
else:
    # Create a list out of your dict
    data = [data[i] for i in range(max_key + 1)]

Now you can still use data[4] and get the same result. But now data is a list, not a dictionary.

Notice that you can't create a list if your dictionary is missing some keys in between 0 and max_key.

Edit I see you are converting a dataframe into a list of dictionaries. This is what you should do:

data = df.to_dict('records')
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50