Getting ValueError: DataFrame constructor not properly called while creating a dataframe from lists of list
Getting ValueError: DataFrame constructor not properly called while creating a dataframe from lists of list
我在将列表的列表转换为数据框时遇到以下错误:
引发 ValueError('DataFrame constructor not properly called!')
ValueError:未正确调用 DataFrame 构造函数!
我已经使用 numpy 拆分列表,现在需要将这些列表列表转换为数据框:
import numpy as np
import pandas as pd
def SplitList():
l = np.array([6,2,5,1,3,6,9,7,6])
n = 3
list = l.reshape((len(l)//n), n).T
print(list)
df = pd.DataFrame(list)
首先,不要用list
作为变量名,它是Python中的保留关键字。
其次,您需要 return
重塑数组的函数,因此您需要:
import numpy as np
import pandas as pd
def SplitList():
l = np.array([6,2,5,1,3,6,9,7,6])
n = 3
a = l.reshape((len(l)//n), n).T
return a
df = pd.DataFrame(SplitList())
print(df)
0 1 2
0 6 1 9
1 2 3 7
2 5 6 6
只是一个建议,但可能是一个让您的函数更具可重用性的想法。例如:
def split_list(arr, n):
arr = np.array(arr)
return arr.reshape(-1, n).T
split_list([6,2,5,1,3,6,9,7,6], 3)
[出局]
[[6 1 9]
[2 3 7]
[5 6 6]]
我在将列表的列表转换为数据框时遇到以下错误:
引发 ValueError('DataFrame constructor not properly called!') ValueError:未正确调用 DataFrame 构造函数!
我已经使用 numpy 拆分列表,现在需要将这些列表列表转换为数据框:
import numpy as np
import pandas as pd
def SplitList():
l = np.array([6,2,5,1,3,6,9,7,6])
n = 3
list = l.reshape((len(l)//n), n).T
print(list)
df = pd.DataFrame(list)
首先,不要用list
作为变量名,它是Python中的保留关键字。
其次,您需要 return
重塑数组的函数,因此您需要:
import numpy as np
import pandas as pd
def SplitList():
l = np.array([6,2,5,1,3,6,9,7,6])
n = 3
a = l.reshape((len(l)//n), n).T
return a
df = pd.DataFrame(SplitList())
print(df)
0 1 2
0 6 1 9
1 2 3 7
2 5 6 6
只是一个建议,但可能是一个让您的函数更具可重用性的想法。例如:
def split_list(arr, n):
arr = np.array(arr)
return arr.reshape(-1, n).T
split_list([6,2,5,1,3,6,9,7,6], 3)
[出局]
[[6 1 9]
[2 3 7]
[5 6 6]]