我想做以下事情:
data = [1,2,3,4,5,6,7,8,9]
import pandas as pd
import numpy as np
df = pd.DataFrame(np.array(data).reshape(3,3), columns = list("abc"))
...
a b c
0 1 2 3
1 4 5 6
2 7 8 9
针对:
data = [[[69, 2.0], [69, 2.0], [64, 3.872983346207417], [71, 1.4142135623730951], [80, 4.0]],
[[69, 2.0], [69, 2.0], [64, 3.872983346207417], [71, 1.4142135623730951], [80, 4.0]],
[[69, 2.0], [69, 2.0], [64, 3.872983346207417], [71, 1.4142135623730951], [80, 4.0]]]
import pandas as pd
import numpy as np
df = pd.DataFrame(np.array(data).reshape([[[2], [2], [2], [2], [2]]]), columns = list("abcde"))
但是代码中存在问题:
Traceback (most recent call last):
File "script.py", line 9, in <module>
df = pd.DataFrame(np.array(data).reshape([[[2], [2], [2], [2], [2]]]), columns = list("abcde"))
TypeError: 'list' object cannot be interpreted as an integer
有没有建议?
注意:更大的数据呈指数增长
答案 0 :(得分:2)
喜欢吗?
df = pd.DataFrame(data= data)
df.columns = list("abcde")
输出:
a b c d e
0 [69, 2.0] [69, 2.0] [64, 3.872983346207417] [71, 1.4142135623730951] [80, 4.0]
1 [69, 2.0] [69, 2.0] [64, 3.872983346207417] [71, 1.4142135623730951] [80, 4.0]
2 [69, 2.0] [69, 2.0] [64, 3.872983346207417] [71, 1.4142135623730951] [80, 4.0]
答案 1 :(得分:0)
尝试不添加reshape
pd.DataFrame(data, columns = list("abcde"))
a b ... d e
0 [69, 2.0] [69, 2.0] ... [71, 1.4142135623730951] [80, 4.0]
1 [69, 2.0] [69, 2.0] ... [71, 1.4142135623730951] [80, 4.0]
2 [69, 2.0] [69, 2.0] ... [71, 1.4142135623730951] [80, 4.0]
[3 rows x 5 columns]