从pandas中的行计算中计算最小值

时间:2017-06-07 03:39:05

标签: python pandas

我在pandas DataFrame的一列中有一个吞吐量值列表。我想计算一个值从阈值的变化作为该阈值的百分比。

因此,如果我的阈值为2和7.我想计算以下函数的最小值。

(df.throughput - 2)/2  
(df.throughput - 7)/7

x   throughtput
1   3
4   4
7   9

我尝试使用以下内容创建新列,但我一直收到错误消息。我觉得这里有一些非常明显的东西。

df['pct'] =  np.min(  (df.throughput-2)/2,  (df.throughput - 7)/7  )
df['pct'] =  np.min(  (df['throughput']-2)/2,  (df['throughput'] - 7)/7  )
'Series' objects are mutable, thus they cannot be hashed

2 个答案:

答案 0 :(得分:1)

您可以创建新的a = (df['throughtput'] - 2)/2 b = (df['throughtput'] - 7)/7 df['pct'] = np.where(a < b, a, b) print (df) x throughtput pct 0 1 3 -0.571429 1 4 4 -0.428571 2 7 9 0.285714 ,对它们进行比较并使用numpy.where作为新列:

a = (df['throughtput'] - 2)/2
b = (df['throughtput'] - 7)/7
df['pct'] = pd.concat([a,b], axis=1).min(axis=1)
print (df)
   x  throughtput       pct
0  1            3 -0.571429
1  4            4 -0.428571
2  7            9  0.285714

concatDataFrame.min的解决方案:

a = (df['throughtput'] - 2)/2
b = (df['throughtput'] - 7)/7
df['pct'] = np.amin(np.column_stack([a,b]), axis=1)
print (df)
   x  throughtput       pct
0  1            3 -0.571429
1  4            4 -0.428571
2  7            9  0.285714

或按numpy.column_stack创建二维数组,然后按numpy.amin

获取
a = (df['throughtput'].values - 2)/2
b = (df['throughtput'].values - 7)/7
df['pct'] = np.amin(np.column_stack([a,b]), axis=1)
print (df)
   x  throughtput       pct
0  1            3 -0.571429
1  4            4 -0.428571
2  7            9  0.285714
N = 1000000
#N = 10
df = pd.DataFrame({'x': np.random.randint(10,size=N),
                   'throughtput':np.random.randint(10,size=N)})
print (df)

In [50]: %%timeit
    ...: a = (df['throughtput'] - 2)/2
    ...: b = (df['throughtput'] - 7)/7
    ...: df['pct'] = np.where(a < b, a, b)
    ...: 
10 loops, best of 3: 21.1 ms per loop

In [51]: %%timeit
    ...: a = (df['throughtput'] - 2)/2
    ...: b = (df['throughtput'] - 7)/7
    ...: df['pct'] = pd.concat([a,b], axis=1).min(axis=1)
    ...: 
10 loops, best of 3: 56.4 ms per loop

In [52]: %%timeit
    ...: a = (df['throughtput'] - 2)/2
    ...: b = (df['throughtput'] - 7)/7
    ...: df['pct'] = np.amin(np.column_stack([a,b]), axis=1)
    ...: 
10 loops, best of 3: 35.1 ms per loop


In [53]: %%timeit
    ...: a = (df['throughtput'].values - 2)/2
    ...: b = (df['throughtput'].values - 7)/7
    ...: df['pct'] = np.amin(np.column_stack([a,b]), axis=1)
    ...: 
10 loops, best of 3: 38.5 ms per loop

<强>计时

In [54]: %%timeit
    ...: df['cal_1'] = (df.throughtput - 2)/2
    ...: df['cal_2'] = (df.throughtput - 7)/7
    ...: df['pct'] = df[['cal_1','cal_2']].min(axis=1)
    ...: df[['x','throughtput','pct']]
    ...: 
10 loops, best of 3: 73.7 ms per loop

In [55]: %%timeit
    ...: df['pct']=[min(i,j) for i,j in (zip((df.throughtput - 2)/2,(df.throughtput - 7)/7))]
    ...: 
1 loop, best of 3: 435 ms per loop

Tiny.D的另一个答案:

{{1}}

答案 1 :(得分:0)

试试这个:

df['pct'] = [min(i,j) for i,j in (zip((df.throughput - 2)/2,(df.throughput - 7)/7))]
df

输出将是:

    throughput  x   pct
0   3           1   -0.571429
1   4           4   -0.428571
2   9           7   0.285714

或:

df['cal_1'] = (df.throughput - 2)/2
df['cal_2'] = (df.throughput - 7)/7
df['pct'] = df[['cal_1','cal_2']].min(axis=1)
df[['x','throughput','pct']]

输出将是:

    x   throughput  pct
0   1   3           -0.571429
1   4   4           -0.428571
2   7   9           0.285714