数据框是:
date ids_x ids_y
0 2011-04-23 [0, 1, 2, 10, 11, 12, 13] []
1 2011-04-24 [0, 1, 2, 10, 11, 12, 13] [12,4]
2 2011-04-25 [0, 1, 2, 3, 4, 1, 12] []
3 2011-04-26 [0, 1, 2, 3, 4, 5, 6] [4,5,6]
方便但较慢的方法是使用:
df['ids'] = df['ids_x'] + df['ids_y']
我希望通过使用numpy
实现这种方法,但目前速度非常慢,需要4秒
。因为Pandas
使用numpy,所以我认为应该在不使用Pandas
的情况下使用numpy以减少开销。
我使用了column_stack
,但输出结果如下:
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[9,8,7],[6,5,4,6,7,8]])
np.column_stack((a,b))
[out]: array([[1, 2, 3, [9, 8, 7]], [4, 5, 6, [6, 5, 4, 6, 7, 8]]], dtype=object)
[np.append(x,y) for x, y in zip(a, b)]
行得通吗? - Zerodf['something'] + df['another_thing']
可以解决我的问题,而且不会增加代码复杂度。 - Alireza