0

I am converting my Python dictionary of "user-ratings" into a Pandas dataframe.

Here is the dictionary (user_dict) for reference:

{'U1': [3, 4, 2, 5, 0, 4, 1, 3, 0, 0, 4], 'U2': [2, 3, 1, 0, 3, 0, 2, 0, 0, 3, 0], 'U3': [0, 4, 0, 5, 0, 4, 0, 3, 0, 2, 4], 'U4': [0, 0, 2, 1, 4, 3, 2, 0, 0, 2, 0], 'U5': [0, 0, 0, 5, 0, 4, 0, 3, 0, 0, 4], 'U6': [2, 3, 4, 0, 3, 0, 3, 0, 3, 4, 0], 'U7': [0, 4, 3, 5, 0, 5, 0, 0, 0, 0, 4], 'U8': [4, 3, 0, 3, 4, 2, 2, 0, 2, 3, 2], 'U9': [0, 2, 0, 3, 1, 0, 1, 0, 0, 2, 0], 'U10': [0, 3, 0, 4, 3, 3, 0, 3, 0, 4, 4], 'U11': [2, 2, 1, 2, 1, 0, 2, 0, 1, 0, 2], 'U12': [0, 4, 4, 5, 0, 0, 0, 3, 0, 4, 5], 'U13': [3, 3, 0, 2, 2, 3, 2, 0, 2, 0, 3], 'U14': [0, 3, 4, 5, 0, 5, 0, 0, 0, 4, 0], 'U15': [2, 0, 0, 3, 0, 2, 2, 3, 0, 0, 3], 'U16': [4, 4, 0, 4, 3, 4, 0, 3, 0, 3, 0], 'U17': [0, 2, 0, 3, 1, 0, 2, 0, 1, 0, 3], 'U18': [2, 3, 1, 0, 3, 2, 3, 2, 0, 2, 0], 'U19': [0, 5, 0, 4, 0, 3, 0, 4, 0, 0, 5], 'U20': [0, 0, 3, 0, 3, 0, 4, 0, 2, 0, 0], 'U21': [3, 0, 2, 4, 2, 3, 0, 4, 2, 3, 3], 'U22': [4, 4, 0, 5, 3, 5, 0, 4, 0, 3, 0], 'U23': [3, 0, 0, 0, 3, 0, 2, 0, 0, 4, 0], 'U24': [4, 0, 3, 0, 3, 0, 3, 0, 0, 2, 2], 'U25': [0, 5, 0, 3, 3, 4, 0, 3, 3, 4, 4]}

When I run this code:

df=  pd.DataFrame([user_dict], columns=user_dict.keys())
print(df)

The dataframe prints all on one row. How do I get it to print with each user on its own row? With two columns... User and User Ratings?

2 Answers 2

4

You don't need to give keys when you are converting dictionary to dataframe. The dictionary keys are taken as columns.

In [75]: d = {'U1': [3, 4, 2, 5, 0, 4, 1, 3, 0, 0, 4], 'U2': [2, 3, 1, 0, 3, 0, 2, 0, 0, 3, 0], 'U3': [0, 4, 0, 5, 0,
    ...: 4, 0, 3, 0, 2, 4], 'U4': [0, 0, 2, 1, 4, 3, 2, 0, 0, 2, 0], 'U5': [0, 0, 0, 5, 0, 4, 0, 3, 0, 0, 4], 'U6': [2
    ...: , 3, 4, 0, 3, 0, 3, 0, 3, 4, 0], 'U7': [0, 4, 3, 5, 0, 5, 0, 0, 0, 0, 4], 'U8': [4, 3, 0, 3, 4, 2, 2, 0, 2, 3
    ...: , 2], 'U9': [0, 2, 0, 3, 1, 0, 1, 0, 0, 2, 0], 'U10': [0, 3, 0, 4, 3, 3, 0, 3, 0, 4, 4], 'U11': [2, 2, 1, 2,
    ...: 1, 0, 2, 0, 1, 0, 2], 'U12': [0, 4, 4, 5, 0, 0, 0, 3, 0, 4, 5], 'U13': [3, 3, 0, 2, 2, 3, 2, 0, 2, 0, 3], 'U1
    ...: 4': [0, 3, 4, 5, 0, 5, 0, 0, 0, 4, 0], 'U15': [2, 0, 0, 3, 0, 2, 2, 3, 0, 0, 3], 'U16': [4, 4, 0, 4, 3, 4, 0,
    ...:  3, 0, 3, 0], 'U17': [0, 2, 0, 3, 1, 0, 2, 0, 1, 0, 3], 'U18': [2, 3, 1, 0, 3, 2, 3, 2, 0, 2, 0], 'U19': [0,
    ...: 5, 0, 4, 0, 3, 0, 4, 0, 0, 5], 'U20': [0, 0, 3, 0, 3, 0, 4, 0, 2, 0, 0], 'U21': [3, 0, 2, 4, 2, 3, 0, 4, 2, 3
    ...: , 3], 'U22': [4, 4, 0, 5, 3, 5, 0, 4, 0, 3, 0], 'U23': [3, 0, 0, 0, 3, 0, 2, 0, 0, 4, 0], 'U24': [4, 0, 3, 0,
    ...:  3, 0, 3, 0, 0, 2, 2], 'U25': [0, 5, 0, 3, 3, 4, 0, 3, 3, 4, 4]}
    ...:

In [76]: df = pd.DataFrame(d)

In [77]: df
Out[77]:
    U1  U2  U3  U4  U5  U6  U7  U8  U9  U10  U11  U12  ...  U14  U15  U16  U17  U18  U19  U20  U21  U22  U23  U24  U25
0    3   2   0   0   0   2   0   4   0    0    2    0  ...    0    2    4    0    2    0    0    3    4    3    4    0
1    4   3   4   0   0   3   4   3   2    3    2    4  ...    3    0    4    2    3    5    0    0    4    0    0    5
2    2   1   0   2   0   4   3   0   0    0    1    4  ...    4    0    0    0    1    0    3    2    0    0    3    0
3    5   0   5   1   5   0   5   3   3    4    2    5  ...    5    3    4    3    0    4    0    4    5    0    0    3
4    0   3   0   4   0   3   0   4   1    3    1    0  ...    0    0    3    1    3    0    3    2    3    3    3    3
5    4   0   4   3   4   0   5   2   0    3    0    0  ...    5    2    4    0    2    3    0    3    5    0    0    4
6    1   2   0   2   0   3   0   2   1    0    2    0  ...    0    2    0    2    3    0    4    0    0    2    3    0
7    3   0   3   0   3   0   0   0   0    3    0    3  ...    0    3    3    0    2    4    0    4    4    0    0    3
8    0   0   0   0   0   3   0   2   0    0    1    0  ...    0    0    0    1    0    0    2    2    0    0    0    3
9    0   3   2   2   0   4   0   3   2    4    0    4  ...    4    0    3    0    2    0    0    3    3    4    2    4
10   4   0   4   0   4   0   4   2   0    4    2    5  ...    0    3    0    3    0    5    0    3    0    0    2    4

[11 rows x 25 columns]

In [78]: df.T
Out[78]:
     0  1  2  3  4  5  6  7  8  9  10
U1   3  4  2  5  0  4  1  3  0  0   4
U2   2  3  1  0  3  0  2  0  0  3   0
U3   0  4  0  5  0  4  0  3  0  2   4
U4   0  0  2  1  4  3  2  0  0  2   0
U5   0  0  0  5  0  4  0  3  0  0   4
U6   2  3  4  0  3  0  3  0  3  4   0
U7   0  4  3  5  0  5  0  0  0  0   4
U8   4  3  0  3  4  2  2  0  2  3   2
U9   0  2  0  3  1  0  1  0  0  2   0
U10  0  3  0  4  3  3  0  3  0  4   4
U11  2  2  1  2  1  0  2  0  1  0   2
U12  0  4  4  5  0  0  0  3  0  4   5
U13  3  3  0  2  2  3  2  0  2  0   3
U14  0  3  4  5  0  5  0  0  0  4   0
U15  2  0  0  3  0  2  2  3  0  0   3
U16  4  4  0  4  3  4  0  3  0  3   0
U17  0  2  0  3  1  0  2  0  1  0   3
U18  2  3  1  0  3  2  3  2  0  2   0
U19  0  5  0  4  0  3  0  4  0  0   5
U20  0  0  3  0  3  0  4  0  2  0   0
U21  3  0  2  4  2  3  0  4  2  3   3
U22  4  4  0  5  3  5  0  4  0  3   0
U23  3  0  0  0  3  0  2  0  0  4   0
U24  4  0  3  0  3  0  3  0  0  2   2

df.T - transpose of dataframe

Sign up to request clarification or add additional context in comments.

Comments

0

You could also melt down the DataFrame after creating the DataFrame from the dictionary to have the two final columns

# Create DataFrame from user_dict 
df = pd.DataFrame(user_dict)

# melt down DataFrame and rename columns accordingly
final_df = pd.melt(df,var_name='User',value_name='User Ratings')

Comments

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.