将两列数字合并成一个数组

5

创建示例数据框的代码:

Sample = [{'account': 'Jones LLC', 'Jan': 150, 'Feb': 200, 'Mar': [[.332, .326], [.058, .138]]},
     {'account': 'Alpha Co',  'Jan': 200, 'Feb': 210, 'Mar': [[.234, .246], [.234, .395]]},
     {'account': 'Blue Inc',  'Jan': 50,  'Feb': 90,  'Mar': [[.084, .23], [.745, .923]]}]
df = pd.DataFrame(Sample)

示例数据框可视化:

 df:
  account        Jan      Feb          Mar
Jones LLC  |     150   |   200    | [.332, .326], [.058, .138]
Alpha Co   |     200   |   210    | [[.234, .246], [.234, .395]
Blue Inc   |     50    |   90     | [[.084, .23], [.745, .923]

我正在寻找一种公式,将1月和2月的列合并成一个数组,并在新列中输出此数组。
期望输出:
 df:
  account        Jan      Feb          Mar                             New
Jones LLC  |     150   |   200    | [.332, .326], [.058, .138]   |    [150, 200]
Alpha Co   |     200   |   210    | [[.234, .246], [.234, .395]  |    [200, 210]
Blue Inc   |     50    |   90     | [[.084, .23], [.745, .923]   |    [50, 90]

1
应用速度要注意。请查看我的帖子以比较两者。 - piRSquared
3个回答

8

使用values.tolist

df.assign(New=df[['Feb', 'Jan']].values.tolist())
# inplace... use this
# df['New'] = df[['Feb', 'Jan']].values.tolist()

   Feb  Jan                               Mar    account         New
0  200  150  [[0.332, 0.326], [0.058, 0.138]]  Jones LLC  [200, 150]
1  210  200  [[0.234, 0.246], [0.234, 0.395]]   Alpha Co  [210, 200]
2   90   50   [[0.084, 0.23], [0.745, 0.923]]   Blue Inc    [90, 50]

大数据中的时间控制
使用一个有3,000个行的dataframe时,避免使用apply可以提高60倍的速度。

df = pd.concat([df] * 1000, ignore_index=True)

%timeit df.assign(New=df[['Feb', 'Jan']].values.tolist())
%timeit df.assign(New=df.apply(lambda x: [x['Jan'], x['Feb']], axis=1))

1000 loops, best of 3: 947 µs per loop
10 loops, best of 3: 61.7 ms per loop

对于包含30,000行数据的数据框,速度比原来快了160倍

df = pd.concat([df] * 10000, ignore_index=True)

100 loops, best of 3: 3.58 ms per loop
1 loop, best of 3: 586 ms per loop

7

列表推导式

如果你想要更快的速度,这就是最好的选择。

df['New'] = [[x, y] for x, y in zip(df.Jan, df.Feb)]
df

   Feb  Jan                               Mar    account         New
0  200  150  [[0.332, 0.326], [0.058, 0.138]]  Jones LLC  [150, 200]
1  210  200  [[0.234, 0.246], [0.234, 0.395]]   Alpha Co  [200, 210]
2   90   50   [[0.084, 0.23], [0.745, 0.923]]   Blue Inc    [50, 90]

如果您想删除原始列,可以使用:
df.drop(['Jan', 'Feb'], axis=1, inplace=True)

df.applyaxis=1

这里只是为了完整起见,我不再赞成使用apply

df['New'] = df.apply(lambda x: [x['Jan'], x['Feb']], axis=1)    
df

   Feb  Jan                               Mar    account         New
0  200  150  [[0.332, 0.326], [0.058, 0.138]]  Jones LLC  [150, 200]
1  210  200  [[0.234, 0.246], [0.234, 0.395]]   Alpha Co  [200, 210]
2   90   50   [[0.084, 0.23], [0.745, 0.923]]   Blue Inc    [50, 90]

性能
重新运行piR的小数据测试(3000行),包括列表推导式方法,我们得到了以下结果:

%timeit df.assign(New=df[['Feb', 'Jan']].values.tolist())
%timeit df.assign(New=df.apply(lambda x: [x['Jan'], x['Feb']], axis=1))
%timeit df.assign(New=[[x, y] for x, y in zip(df.Jan, df.Feb)])

2.76 ms ± 596 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
152 ms ± 9.47 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
1.59 ms ± 13.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

对于更大的数据(30,000行)——

5.95 ms ± 527 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
1.53 s ± 165 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
8.79 ms ± 793 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

列表解析和.tolist()都是竞争性的方法。你决定使用哪个方法是一种口味问题。不要使用apply


1
谢谢。这个方法有效。我还需要几分钟才能接受答案,但我会的 :) - Ashley O

5

您也可以尝试使用 df['New'] = list(zip(df.Feb, df.Jan))

或者使用 tolist df['New'] = df.ix[:,0:2].values.tolist()


你知道吗,这篇文章让我明白了pandas可以同时友好和残酷。我修改了我的回答。 - cs95

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接