python - Pandas data frame from dictionary -
i have python dictionary of user-item ratings looks this:
sample={'user1': {'item1': 2.5, 'item2': 3.5, 'item3': 3.0, 'item4': 3.5, 'item5': 2.5, 'item6': 3.0}, 'user2': {'item1': 2.5, 'item2': 3.0, 'item3': 3.5, 'item4': 4.0}, 'user3': {'item2':4.5,'item5':1.0,'item6':4.0}}
i looking convert pandas data frame structured like
col1 col2 col3 0 user1 item1 2.5 1 user1 item2 3.5 2 user1 item3 3.0 3 user1 item4 3.5 4 user1 item5 2.5 5 user1 item6 3.0 6 user2 item1 2.5 7 user2 item2 3.0 8 user2 item3 3.5 9 user2 item4 4.0 10 user3 item2 4.5 11 user3 item5 1.0 12 user3 item6 4.0
any ideas appreciated :)
try following code:
import pandas sample={'user1': {'item1': 2.5, 'item2': 3.5, 'item3': 3.0, 'item4': 3.5, 'item5': 2.5, 'item6': 3.0}, 'user2': {'item1': 2.5, 'item2': 3.0, 'item3': 3.5, 'item4': 4.0}, 'user3': {'item2':4.5,'item5':1.0,'item6':4.0}} df = pandas.dataframe([ [col1,col2,col3] col1, d in sample.items() col2, col3 in d.items() ])
Comments
Post a Comment