How to create pandas datafram from a dictionary and keep list in one cell

Viewed 103

I have a dictionary like:

dic = {'c': [14, 41],
       'i': '52983542076720',
       'p': 31.7,
       's': 100,
       't': 1611945588012261376,
       'x': 11}

I tried

pd.DataFrame(dic) 

and

pd.DataFrame.from_dict(dic,, orient='columns', dtype=int, columns=None)

However, they all return a 2 rows dataframe like:

c i p s t x
0 14 52983542076720 31 100 1611945588012261376 11
1 41 52983542076720 31 100 1611945588012261376 11

I actually want to get a dataframe like:

c i p s t x
0 [14,41] 52983542076720 31 100 1611945588012261376 11

any idea about what I should do to get the result?

2 Answers

You can use pandas.Series instead:

pd.Series(dic).to_frame().T

Output:

          c               i     p    s                    t   x
0  [14, 41]  52983542076720  31.7  100  1611945588012261376  11

Try with pd.DataFrame.from_dict

out = pd.DataFrame.from_dict({0:dic},'index')

Out[238]: 
          c               i     p    s                    t   x
0  [14, 41]  52983542076720  31.7  100  1611945588012261376  11
Related