Pandas 从长度不等的列表创建数据帧
我尝试转换这样一个列表:Pandas 从长度不等的列表创建数据帧,pandas,Pandas,我尝试转换这样一个列表: l = [[1, 2, 3, 17], [4, 19], [5]] 一个数据帧,其中每个数字作为标记,列表位置作为值 例如,19在第二个列表中,因此我希望得到一行,其中“19”作为索引,“1”作为值,依此类推 我设法得到了它(参见下面的锅炉板),但我想还有更简单的东西 >>> df=pd.DataFrame(l) >>> df=df.unstack().reset_index(level=0,drop=True) &
l = [[1, 2, 3, 17], [4, 19], [5]]
一个数据帧,其中每个数字作为标记,列表位置作为值
例如,19在第二个列表中,因此我希望得到一行,其中“19”作为索引,“1”作为值,依此类推
我设法得到了它(参见下面的锅炉板),但我想还有更简单的东西
>>> df=pd.DataFrame(l)
>>> df=df.unstack().reset_index(level=0,drop=True)
>>> df=df[df.notnull()==True] # remove NaN rows
>>> df=pd.DataFrame(df)
>>> df = df.reset_index().set_index(0)
>>> print df
index
0
1 0
4 1
5 2
2 0
19 1
3 0
17 0
提前谢谢
In [52]: pd.DataFrame([(item, i) for i, seq in enumerate(l)
for item in seq]).set_index(0)
Out[52]:
1
0
1 0
2 0
3 0
17 0
4 1
19 1
5 2