[英]Python converting dictionary to dataframe fail
When I try to convert the following dictionary to dataframe, python repeats each row twice.当我尝试将以下字典转换为数据帧时,python 将每行重复两次。
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: Python 打印以下输出:
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?有人可以建议我如何解决这个问题吗?
Let's start with an example让我们从一个例子开始
You're getting你得到
pd.DataFrame({'Mini': [1, 1], 'on': [2, 2]})
When you want当你想要的时候
pd.DataFrame({'Mini': [1], 'on': [2]})
You're definition of a
is a 2x2x5x4 array in list form.您定义的a
是列表形式的 2x2x5x4 数组。 The first dimension is getting zipped away into the values of the dict
.第一个维度被压缩到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
第二个维度是长度为 2 的列表,我刚刚演示了将这样的字典传递给pd.DataFrame
时会发生什么
To fix it, swap the following line with your previous definition of d
要修复它,请将以下行与您之前对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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.