如何从返回元组的向量函数中填充两列(col_a,col_b)

时间:2019-02-26 20:49:47

标签: pandas numpy vectorization numpy-broadcasting

下面抛出错误

def test(throwaway):
    return(1,2)

df['prices_study'], df['prices_bench'] = list(map(dates_ok, df.date_announce))

我知道我可以单独创建每个列,但是肯定有一种方法可以压缩这个列吗?

1 个答案:

答案 0 :(得分:1)

IIUC:

使用splat解压缩map中的zip对象

df = pd.DataFrame(1, range(4), [*'abc'])

def test(_): return (2, 3)

df['d'], df['e'] = zip(*map(test, df.index))

df

   a  b  c  d  e
0  1  1  1  2  3
1  1  1  1  2  3
2  1  1  1  2  3
3  1  1  1  2  3