A1122
A1122

Reputation: 1354

Python converting dictionary to dataframe fail

When I try to convert the following dictionary to dataframe, python repeats each row twice.

a = [[[[130.578125, 96, 130.59375, 541],
       [130.5625, 635, 130.609375, 1055],
       [130.546875, 657, 130.625, 1917],
       [130.53125, 707, 130.640625, 1331],
       [130.515625, 1530, 130.65625, 2104]],
      [[130.578125, 96, 130.59375, 541],
       [130.5625, 635, 130.609375, 1055],
       [130.546875, 657, 130.625, 1917],
       [130.53125, 707, 130.640625, 1331],
       [130.515625, 1530, 130.65625, 2104]]],
     [[[143.34375, 5, 143.359375, 79],
       [143.328125, 142, 143.375, 129],
       [143.3125, 132, 143.390625, 137],
       [143.296875, 126, 143.40625, 118],
       [143.28125, 113, 143.421875, 125]],
      [[143.34375, 5, 143.359375, 79],
       [143.328125, 142, 143.375, 129],
       [143.3125, 132, 143.390625, 137],
       [143.296875, 126, 143.40625, 118],
       [143.28125, 113, 143.421875, 125]]]]

b = ['Mini','on']

c = dict(zip(b,a))

d = pd.DataFrame.from_dict(c)

print d

Python prints the following output:

                                                Mini  \
0  [[130.578125, 96, 130.59375, 541], [130.5625, ...
1  [[130.578125, 96, 130.59375, 541], [130.5625, ...

                                                  on
0  [[143.34375, 5, 143.359375, 79], [143.328125, ...
1  [[143.34375, 5, 143.359375, 79], [143.328125, ...

The desired output is:

                                                Mini  \
0  [[130.578125, 96, 130.59375, 541], [130.5625, ...

                                                  on
0  [[143.34375, 5, 143.359375, 79], [143.328125, ...

Can someone please suggest how I can fix this?

Upvotes: 2

Views: 253

Answers (1)

piRSquared
piRSquared

Reputation: 294506

Let's start with an example

You're getting

pd.DataFrame({'Mini': [1, 1], 'on': [2, 2]})

enter image description here

When you want

pd.DataFrame({'Mini': [1], 'on': [2]})

enter image description here


You're definition of a is a 2x2x5x4 array in list form. The first dimension is getting zipped away into the values of the dict. The second dimension is a list of length 2 and I've just demonstrated what happens when you pass such a dictionary to pd.DataFrame

To fix it, swap the following line with your previous definition of d

    d = pd.Series(c).to_frame().T

Response to comment
To print entire cell content

with pd.option_context('display.max_colwidth', -1):
    print d

Upvotes: 1

Related Questions