问题描述
我有一个看起来像这样的pandas.core.frame.DataFrame
:
I have a pandas.core.frame.DataFrame
that looks like this:
0 1
0 [1,2,3] 1
1 [2,2,1] 1
2 [1,2,1] 1
...
最后一列是标签,列"0"下的每个数组应该是给定类的不同数据点.
The last column is the label and each of the arrays under column '0' are supposed to be different datapoints for a given class.
我希望将其转换为:
x0 x1 x2 label
0 1 2 3 1
1 2 2 1 1
2 1 2 1 1
我没有运气尝试过以下内容
I have tried the following with no luck
ds = ds.apply(lambda x: numpy.ravel(x))
这是以下情况的结果,显然这不是正确的方法.
That was result of the following, obviously that is not the right way to do this.
<list>.extend(zip(points,labels))
ds = pandas.core.frame.DataFrame(data=<list>)
感谢您提供任何有关如何修复实际数据集或如何正确创建具有两个列表(点和标签)的数据集的帮助.
Any help is appreciated, on how to fix the actual dataset or create it correctly having the two lists (points and labels).
推荐答案
这就是我的方法.首先删除您的1列(这样我们就不会弄乱名称了):
Here's how I would do it. First remove your 1 column (so we dont mess the naming):
df['id'] = df[1]
df = df.drop(1, axis = 1)
然后使用我们要连接的对象创建一个obj,然后进行concat:
Then create an objs, with what we want to concat, and concat:
objs = [df, pd.DataFrame(df[0].tolist())]
pd.concat(objs, axis=1)
0 id 0 1 2
0 [1, 2, 3] 1 1 2 3
1 [2, 2, 1] 1 2 2 1
2 [1, 2, 1] 1 1 2 1
这篇关于pandas DataFrame爆炸列内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!