让我们说我有两个熊猫数据框X和Y:
X =
+---+----------+---------+
| | Value1 | Value2 |
+---+----------+---------+
| A | 1 | NaN |
| B | 0 | 0 |
+---+----------+---------+
Y =
+---+----------+---------+
| | Value1 | Value2 |
+---+----------+---------+
| A | 2 | NaN |
| C | 30 | NaN |
+---+----------+---------+
我想根据产生的索引(行名)合并/加入它们:
+---+----------+---------+
| | Value1 | Value2 |
+---+----------+---------+
| A | 1 | 2 |
| B | 0 | 0 |
| C | 30 | NaN |
+---+----------+---------+
使用合并和“外部”,结果表在每个表中都有列,而不仅仅是串联。我需要将新行追加到末尾的内容,还需要为匹配索引追加新列。
这是“外部”合并的结果:
+---+----------+---------+----------+---------+
| | Value1_X | Value2_X| Value1_Y | Value2_Y|
+---+----------+---------+----------+---------+
| A | 1 | NaN | 2 | NaN |
| B | 0 | 0 | NaN | NaN |
| C | NaN | NaN | 30 | NaN |
+---+----------+---------+----------+---------+
几乎是我想要的,但是忽略了原始的列标签...
答案 0 :(得分:2)
关于“外部”合并的结果:
X =
+---+----------+---------+----------+---------+
| | Value1_X | Value2_X| Value1_Y | Value2_Y|
+---+----------+---------+----------+---------+
| A | 1 | NaN | 2 | NaN |
| B | 0 | 0 | NaN | NaN |
| C | NaN | NaN | 30 | NaN |
+---+----------+---------+----------+---------+
做,X = X.apply(lambda x: pd.Series(x.dropna().values), axis = 1)
这将给
0 1
A 1.0 2.0
B 0.0 0.0
C 30.0 NaN