Python 数组长度与混合列表列和数据帧列的索引长度不匹配
我有两个数据帧和一个列表。我想把它们混合在一个数据框中 列表A m1,数据帧测试_子数据和数据帧预测:Python 数组长度与混合列表列和数据帧列的索引长度不匹配,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个数据帧和一个列表。我想把它们混合在一个数据框中 列表A m1,数据帧测试_子数据和数据帧预测: len(m1) 438 test_subdata.shape (438, 8) predicciones.shape (438, 3) 基本上我想这样做,一个大小为(438,3)的数据帧,上面的值是: result_frame = pd.DataFrame({'index': test_subdata['id'], 'match_1': m1,
len(m1)
438
test_subdata.shape
(438, 8)
predicciones.shape
(438, 3)
基本上我想这样做,一个大小为(438,3)的数据帧,上面的值是:
result_frame = pd.DataFrame({'index': test_subdata['id'], 'match_1': m1,
'pred1': predicciones['pred1']})
但当我这样做时,会出现以下错误:
ValueError: array length 438 does not match index length 841
一些想法,发生了什么
PS:当我将一个数据帧与一个列表混合时,即使在两个数据帧之间,一切都正常。由于该系列包含的索引,您将获得数组不匹配错误。因此,提前重置索引或只传递值,即
result_frame = pd.DataFrame({'index': test_subdata['id'].values, 'match_1': m1,
'pred1': predicciones['pred1'].values})
解释
由于test\u子数据
和prediccions
是系列,如果test\u子数据
和prediccions
的索引不同,则将从数据帧构造函数创建一个不存在索引的新对象。因此,在这种情况下,数据帧的大小是原来的两倍。(要使现有方法有效,请确保两个数据帧具有相同的索引。)
由于
m1
长度与现有索引长度不匹配,因此将出现数组长度不匹配错误 高兴可以帮助@AndrésCórdova