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?
1 Answer
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
2 Comments
Vijayendra
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?
jezrael
I add last solution, I believe it can be fastest.
df = pd.DataFrame(d)