2

I have a python dictionary with key and values and I wish to create a new pandas data frame object with a new column constructed from dictionary values only. Whats is the most optimised way to achieve this?

3
  • 1
    Can you add sample? Maybe works simple df = pd.DataFrame(d) Commented Dec 13, 2016 at 18:44
  • For example, I have a dictionary as name (key) and score(value) [tom:100, jerry:150, micky:400, donald:250] Now all I want is a data frame with column name as "scores" having all scores(values) from this dictionary. Say like df['scores'] = ... Commented Dec 13, 2016 at 18:54
  • No, put the example in the original question not in a comment. Commented Dec 13, 2016 at 19:07

1 Answer 1

4

I think you need DataFrame.from_dict:

d = {'tom':100, 'jerry':150, 'micky':400, 'donald':250}

df = pd.DataFrame.from_dict(d, orient='index')
df.columns = ['scores']
print (df)
        scores
micky      400
donald     250
jerry      150
tom        100

Another solution is add dict with new column name to DataFrame constructor:

df = pd.DataFrame({'scores':d})

print (df)
        scores
donald     250
jerry      150
micky      400
tom        100

Last if need remove names from index add reset_index:

print (df.reset_index(drop=True))
   scores
0     400
1     250
2     150
3     100

Another solution is use only values of dict, but python dictionaries are always unordered, so get random order of values in df:

print (d.values())
dict_values([400, 250, 150, 100])

#python 3 add list
df = pd.DataFrame({'scores': list(d.values())})
print (df)
   scores
0     400
1     250
2     150
3     100
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @jezrael for your quick reply. I wish to achieve outcome as shown by your last step - without names. I am looking for an optimised way to achieve this as my example was a small dataset. What if we have a huge dataset and yet we first load whole dictionary into data frame and then drop names. Can we directly load values into data frame?
I add last solution, I believe it can be fastest.

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.