Pandas应用错误我有一个Pandas数据框,包含以下各列
col = ["File_Path", "Function_Body", "Prediction", "Line_Number"]
我在列函数体上应用get_prediction()函数,它返回三个值
1.列表(预测):例如[1,1,0,0,0]
1.列表(置信度):例如[64.000, 88.000,0,0,0]
1.列表的列表(前5个标记及行号):[['int', 5], ['ret', 6],['char', 5],['sum', 4],['i', 2]]
下面的代码段对于一个数据集运行良好,但对于另一个数据集却出现错误。
final_df["Prediction"], final_df["Confidence"], final_df["Tokens"] = zip(*final_df["Function_Body"].apply(lambda x:get_prediction(x)))
错误:
File "nginx_fast.py", line 404, in <module>
final_df["Prediction"], final_df["Confidence"], final_df["Tokens"] = zip(*final_df["Function_Body"].apply(lambda x:get_prediction(x)))
File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 2938, in __setitem__
self._set_item(key, value)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 3001, in _set_item
NDFrame._set_item(self, key, value)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py", line 3624, in _set_item
self._data.set(key, value)
File "/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py", line 1067, in set
"Shape of new values must be compatible with manager shape"
AssertionError: Shape of new values must be compatible with manager shape
我在return语句之前检查了返回值的形状和类型,它看起来很好。
1条答案
按热度按时间w8f9ii691#
我也遇到过这个问题,我是这样解决的
因为输出'c'是一个元组,DataFrame应该得到一个列表,所以应该将元组转换为列表。