【发布时间】:2018-11-12 17:28:54
【问题描述】:
我有一个问题要在同一个数据框(start_end)中将两列合并为一列,同时删除空值。我打算将“起始站”和“结束站”合并为“站”,并根据新的“站”列保留“持续时间”。我已经尝试过 pd.merge、pd.concat、pd.append,但我无法解决。
Start_end 的数据帧:
Duration End station Start station
14 1407 NaN 14th & V St NW
19 509 NaN 21st & I St NW
20 638 15th & P St NW. NaN
27 1532 NaN Massachusetts Ave & Dupont Circle NW
28 759 NaN Adams Mill & Columbia Rd NW
预期输出:
Duration stations
14 1407 14th & V St NW
19 509 21st & I St NW
20 638 15th & P St NW
27 1532 Massachusetts Ave & Dupont Circle NW
28 759 Adams Mill & Columbia Rd NW
到目前为止我的代码:
#start_end is the dataframe, 'start station', 'end station', 'duration'
start_end = pd.concat([df_start, df_end])
这是我试图做的:
station = pd.merge([start_end['Start station'],start_end['End station']])
【问题讨论】:
-
数据框作为文本,而不是图像。
-
在列的上下文中定义“合并”。请提供带有数据、输入和预期输出的minimal reproducible example。你的形象并没有真正的帮助。
-
您希望结果是什么样的?
End_Station是否有除NaN以外的值,如果有,它们如何与Start_Station值结合? -
我提取/描述数据帧结构的更好方法(什么代码)是什么?
标签: python pandas dataframe merge append