简体   繁体   English

使用熊猫取消嵌套数组列

[英]Un-nesting array column using pandas

My co-worker has handed me a dataset containing a column with nested data: 我的同事已将包含嵌套数据列的数据集交给我:

id | ... | x
 0 | ... | [['a',3.0],['b',5.4]]
 1 | ... | [['a',1.3],['b',7.6]]
 2 | ... | [['b',2.4],['a',8.8]]
 : | ... | :                   :

I am trying to parse the column to multiple columns to get something like this: 我正在尝试将该列解析为多个列,以获得类似这样的信息:

id | ... | a   | b
 0 | ... | 3.0 | 5.4
 1 | ... | 1.3 | 7.6
 2 | ... | 8.8 | 2.4
 : | ... | :   | :

unfortunately I can't seem to find the way to do this using pandas. 不幸的是,我似乎找不到使用熊猫的方法。 I know that nesting data this way is to be discouraged but unfortunately my co-worker doesn't perceive this to be a problem so this is how I'm spending my weekend. 我知道不希望以这种方式嵌套数据,但是不幸的是我的同事并不认为这是一个问题,所以这就是我度过周末的方式。

Does anyone have experience handling this type of problem? 有没有人有处理此类问题的经验?

I think this should work: 我认为这应该工作:

records = [ dict(row) for row in df['x'].values ]
new_df = pd.DataFrame.from_records(records, columns = records[0].keys(), index = df.index)

(I did not test it, so it might required some changes) (我没有对其进行测试,因此可能需要进行一些更改)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM