【发布时间】:2021-11-22 23:01:50
【问题描述】:
已编辑:
我有一个熊猫数据框如下:
Class Sex SibSp Fare
0 0 0 0 0
2 2 2 2 2
3 3 3 3 3
5 5 5 5 5
我有另一个熊猫数据框如下:
Class Sex SibSp Fare
1 1 1 1 1
4 4 4 4 4
如果我使用连接这两个数据框
pd.concat([traindf,testdf])
我得到以下结果:
Class Sex SibSp Fare
0 0 0 0 0
2 2 2 2 2
3 3 3 3 3
5 5 5 5 5
1 1 1 1 1
4 4 4 4 4
但是,我想得到如下结果:
Class Sex SibSp Fare
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
我使用过pd.concat([traindf,testdf]).sort_values(),但这不起作用。关于如何实现这一点的任何想法,以便数据帧根据它们的索引号连接起来。谢谢
【问题讨论】:
-
你能从样本数据中添加预期的输出吗?是否可以在示例数据中添加
Age列? Please don't post images of code/data (or links to them) -
不,我无法将
Age列添加到样本数据,因为我使用基于数据框中其他可用特征的线性回归填充年龄值 -
您也不能通过直接处理 df[df['Age'].isnull()] 来将 df 切入 2。
-
我不得不将 df 分割成 2,因为我在 Age 值不为 null 的行上训练线性回归,然后尝试预测 Age 值为 null 的第二个 df 上的 Age 值。如果我没有将 df 切片为 2,我将无法训练线性回归模型,因为 NULL 值
标签: python python-3.x pandas dataframe concatenation