Python 将列表列表转换为数据帧
如何转换此列表(或数组)列表 到这个数据帧Python 将列表列表转换为数据帧,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,如何转换此列表(或数组)列表 到这个数据帧 ClstId ColInt ColFloat 1 1538 0.9995334 1 6323 0.9995334 2 7694 0.99999015 2 7862 0.99997352 2 8399 0.99997993 2 9158 0.99996219 使用简单的迭代 演示: import pandas as pd import num
ClstId ColInt ColFloat
1 1538 0.9995334
1 6323 0.9995334
2 7694 0.99999015
2 7862 0.99997352
2 8399 0.99997993
2 9158 0.99996219
使用简单的迭代 演示:
import pandas as pd
import numpy as np
l = [((1538, 6323), (0.9995334, 0.9995334)), ((7694, 7862, 8399, 9158), np.array([0.99999015, 0.99997352, 0.99997993, 0.99996219]))]
d = {"ColInt": [], "ColFloat" : [], "ClstId": []}
for i, v in enumerate(l, 1): #use enumerate to get ClstId
d["ColInt"].extend(list(v[0]))
d["ColFloat"].extend(list(v[1]))
d["ClstId"].extend([i]*len(v[0]))
df = pd.DataFrame(d)
print(df)
ClstId ColFloat ColInt
0 1 0.999533 1538
1 1 0.999533 6323
2 2 0.999990 7694
3 2 0.999974 7862
4 2 0.999980 8399
5 2 0.999962 9158
输出:
import pandas as pd
import numpy as np
l = [((1538, 6323), (0.9995334, 0.9995334)), ((7694, 7862, 8399, 9158), np.array([0.99999015, 0.99997352, 0.99997993, 0.99996219]))]
d = {"ColInt": [], "ColFloat" : [], "ClstId": []}
for i, v in enumerate(l, 1): #use enumerate to get ClstId
d["ColInt"].extend(list(v[0]))
d["ColFloat"].extend(list(v[1]))
d["ClstId"].extend([i]*len(v[0]))
df = pd.DataFrame(d)
print(df)
ClstId ColFloat ColInt
0 1 0.999533 1538
1 1 0.999533 6323
2 2 0.999990 7694
3 2 0.999974 7862
4 2 0.999980 8399
5 2 0.999962 9158
将列表理解与展开结合使用:
a = [((1538, 6323), (0.9995334, 0.9995334)), ((7694, 7862, 8399, 9158),
np.array([0.99999015, 0.99997352, 0.99997993, 0.99996219]))]
L = [(i, y[0],y[1]) for i, x in enumerate(a, 1) for y in zip(x[0], x[1])]
df = pd.DataFrame(L, columns=['ClstId','ColInt','ColFloat'])
print (df)
ClstId ColInt ColFloat
0 1 1538 0.999533
1 1 6323 0.999533
2 2 7694 0.999990
3 2 7862 0.999974
4 2 8399 0.999980
5 2 9158 0.999962
试试这个
In [18]: a = sum([zip(i[0],i[1]) for i in lst],[])
In [20]: df.DataFrame(a, columns=['ColInt','ColFloat'])
Out[20]:
ColInt ColFloat
0 1538 0.999533
1 6323 0.999533
2 7694 0.999990
3 7862 0.999974
4 8399 0.999980
5 9158 0.999962
只能接受一个答案。@mvh-LAYD可以帮助您!天气真好!谢谢你的解决方案。它解决了我的问题,但标记的答案快了一点