2 回答

TA贡献1789条经验 获得超10个赞
对我来说,[]如果使用嵌套lists 和嵌套列表的最大长度与列数相同(此处为 5),则从 DataFrame 构造函数中删除:
def save_data(data):
df = pd.DataFrame(data=data, columns=['Send/Collect', 'Hospital',
'Courier', 'Kit', 'Manufacturer'])
return df
L = [["One", "Two","Three", "Four", "Five"],
["One", "Two","Three", "Four", "Five"],
["One", "Two","Three", "Four", "Five"]]
df = save_data(L)
print (df)
Send/Collect Hospital Courier Kit Manufacturer
0 One Two Three Four Five
1 One Two Three Four Five
2 One Two Three Four Five
您还可以创建检查条件:
def save_data(data):
if max(len(x) for x in L) == 5:
df = pd.DataFrame(data=data, columns=['Send/Collect', 'Hospital', 'Courier',
'Kit', 'Manufacturer'])
return df
L = [["One", "Two","Three", "Four", "Five"],
["One", "Two","Three", "Four", "Five"],
["One", "Two","Three", "Four"]]
df = save_data(L)
print (df)
Send/Collect Hospital Courier Kit Manufacturer
0 One Two Three Four Five
1 One Two Three Four Five
2 One Two Three Four None

TA贡献1111条经验 获得超0个赞
您从数据中删除括号,例如。
def save_data(data):
df = pd.DataFrame(data=data, columns=['Send/Collect',
'Hospital',
'Courier',
'Kit',
'Manufacturer'])
return df
添加回答
举报