0

I am trying to use Pandas to display a dictionary

import pandas as pd

I am getting the error message

ValueError: If using all scalar values, you must pass an index

My dictionary looks like this

{0: "Classifier: SVC(kernel='linear') Time: 6.324216842651367", 1: "Classifier: SVC(kernel='poly') Time: 6.96935248374939", 2: "Classifier: SVC(kernel='sigmoid') Time: 12.137079954147339"}

I think I have found out what the error is. I need to change the data and pass indeces. But I am not sure how to do this, as my dictionary is created in a loop like this:

    for x in range(len(arrL)): 
       ....
       dictionary[x] = "Classifier: "+str(arrL[x]) + " Time: " + str(times[x]) 
pd.DataFrame(dictionary)

If I should/can somehow remove the indeces/numbers to help the problem, that would be find, unless a better solution is adviced

The code that gives me the error is:

pd.DataFrame(dictionary)

---------------------------------Update-----------------------------------

Another problem is that I need something similar to .append() or += to the dictionary

for x in range(len(objList)): 
    
    arrL[x].fit(X_train, c_train)
    ...
    dictionary = {
                "Classifier": arrL[x],
                "Time": times[x],
    }

Notice this approach is different from the original way I created the dictionary:

dictionary[x] = "Classifier: " + os.linesep +str(objList[x]) + " Time: " + str(times[x])  

I need to add all the objects to the dictionary. Right now they are just been overwritten as the loop continues, and at the end only the last object from the list is in the dictionary. I need something like dictionary += ..

user1960836
  • 1,732
  • 7
  • 27
  • 47

2 Answers2

1

I think you just need to give provide an index when passing your dictionary (Shown below). But this is probably not what you want.

d = {0: "Classifier: SVC(kernel='linear') Time: 6.324216842651367", 
     1: "Classifier: SVC(kernel='poly') Time: 6.96935248374939",
     2: "Classifier: SVC(kernel='sigmoid') Time: 12.137079954147339"}

pd.DataFrame(d, index=[key for key in d])

It seems like what you are actually trying to do is something like this...???

Here I have passed the column names as keys and the values in a list.

d = {"Classifier":[ "SVC(kernel='linear')", "SVC(kernel='poly')", "SVC(kernel='sigmoid')"], 
     "Time": ["6.324216842651367", "6.96935248374939", "12.137079954147339"]}

pd.DataFrame(d)

enter image description here

If you have a large dictionary that you need to restructure you can use the following code to get the same result as the previous example.

d = {0: "Classifier: SVC(kernel='linear') Time: 6.324216842651367", 
     1: "Classifier: SVC(kernel='poly') Time: 6.96935248374939",
     2: "Classifier: SVC(kernel='sigmoid') Time: 12.137079954147339"}

# Create a dictionary with the columns you want
new_d = {'Classifier':[], 'Time':[]}

# split each value in the dictionary provided
# take the items at index 1 and append to classifier key
# take the items at index 3 and append to time key
for key in d:
    split_list = d[key].split(' ')
    new_d['Classifier'].append(split_list[1])
    new_d['Time'].append(split_list[3])
    
    
df = pd.DataFrame(new_d)
Bradon
  • 213
  • 2
  • 8
  • Thanks, the image you provided here is exactly the way I want it, so it is divided in 2 columns. But I don't have the dictionary at hand, so I can't do: add the `[ ]` so that I can make a separate column – user1960836 Sep 29 '20 at 20:57
  • 1
    Are you able to access the dictionary at all? If so, I add a little bit of code so that you can modify the dictionary in your example to a dictionary that is more user friendly. Does that work? If not, how are you accessing the values in your dictionary? – Bradon Sep 29 '20 at 21:51
  • Yes I have access, as I wrote the code. I added in the post how I create the dictionary – user1960836 Sep 29 '20 at 22:02
  • 1
    I was confused when you stated "I don't have the dictionary at hand." In my updated response there is an example on how to add new values to a dictionary - use dict[key].append(value) – Bradon Sep 29 '20 at 22:20
1

This is all you need

pd.DataFrame(dictionary, index=[0])

Or if you want them as rows, just add a .T, like this

pd.DataFrame(dictionary, index=[0]).T
callmeanythingyouwant
  • 1,789
  • 4
  • 15
  • 40