我正在尝试使用concat、append或merge来堆叠两个3列数据帧。结果是一个5列的数据帧,其中原始列的位置顺序不同。以下是我尝试过的一些方法:
dfTrain = pd.read_csv("agr_hi_train.csv")
dfTrain2 = pd.read_csv("english/agr_en_train.csv")
dfTrain2.reset_index()
frames = [dfTrain, dfTrain2]
test = dfTrain2.append(dfTrain, ignore_index=True)
test2 = dfTrain2.append(dfTrain)
test3 = pd.concat(frames, axis=0, ignore_index=True)
test4 = pd.merge(dfTrain,dfTrain2, right_index=True, left_index=True)
结果如下:
print(dfTrain.shape)
print(dfTrain2.shape)
print(test.shape)
print(test2.shape)
print(test3.shape)
print(test4.shape)
输出为:
(20198年5月) (20198, 5) (11998, 6) (8200, 6) (8200, 3) (11998年3月)
我希望结果是:
(20198,3)#即最后两个堆叠在一起。 你知道我为什么要增加专栏吗
如果您有不同的列名,那么您的append将分隔这些列。例如:
具有输出:
如果重命名两个dataframes中的列以匹配,那么它将对齐
相关问题 更多 >
编程相关推荐