Pandas数据框架- lambda演算和每个序列的最小值



我有一个包含3列,count_id, AMV和时间的csv。

我正在使用pandas,并将其作为数据帧读取。

results= pd.read_csv('./output.csv')

首先,我首先为count_id排序数据帧,然后为AMV排序。

results_sorted = results.sort_index(by=['count_id','AMV'], ascending=[True, True])

   count_id   AMV  Hour
0    16012E  4004    14
1    16012E  4026    12
2    16012E  4099    15
3    16012E  4167    11
4    16012E  4239    10
5    16012E  4324    13
6    16012E  4941    16
7    16012E  5088    17
8    16012E  5283     9
9    16012E  5620     8
10   16012E  5946    18
11   16012E  6146     7
12   16012W  3622    10
13   16012W  3904    12
14   16012W  3979    11
15   16012W  4076     9
16   16012W  4189    13
17   16012W  4870    14
18   16012W  4899    18
19   16012W  5107    15
20   16012W  5659     8
21   16012W  6325     7
22   16012W  6460    17
23   16012W  6500    16

我现在想对数据执行一些规范化,以便我最终可以在同一图上绘制它。我想做的是找到每个系列(count_id)的AMV的最小值,然后从给定的AMV中减去这个最小值。这将给我一个新的列AMV_norm。

看起来像:

   count_id   AMV  Hour  AMV_norm
0    16012E  4004    14         0
1    16012E  4026    12        22
2    16012E  4099    15        95
3    16012E  4167    11       163
4    16012E  4239    10       235
5    16012E  4324    13       320
6    16012E  4941    16       937
7    16012E  5088    17      1084
8    16012E  5283     9      1279
9    16012E  5620     8      1616
10   16012E  5946    18      1942
11   16012E  6146     7      2142
12   16012W  3622    10         0
13   16012W  3904    12       282
14   16012W  3979    11       357
15   16012W  4076     9       454
16   16012W  4189    13       567
17   16012W  4870    14      1248
18   16012W  4899    18      1277
19   16012W  5107    15      1485
20   16012W  5659     8      2037
21   16012W  6325     7      2703
22   16012W  6460    17      2838
23   16012W  6500    16      2878

我如何定义一个函数,它找到每个系列的最小AMV值,而不是总体的最小AMV值?它看起来像这样:

def minimum_series_value(AMV):
    return AMV.argmin()
然后,我需要创建一个新列,并使用lambda函数填充该行。我知道它看起来像这样:
results_sorted['AMV_norm'] = results_sorted.apply(lambda row:results_sorted(row['AMV']))

从transform中减去AMV列:

In [11]: df.groupby('count_id')["AMV"].transform('min')
Out[11]:
0     4004
1     4004
2     4004
3     4004
4     4004
...
21    3622
22    3622
23    3622
dtype: int64
In [12]: df["AMV"] - df.groupby('count_id')["AMV"].transform('min')
Out[12]:
0        0
1       22
2       95
3      163
4      235
...
21    2703
22    2838
23    2878
dtype: int64
In [13]: df["AMV_norm"] = df["AMV"] - df.groupby('count_id')["AMV"].transform('min')

我相信你想在count_id上分组,然后计算当前值和该组最小值之间的差值。

df['AMV_norm'] = (df.groupby('count_id').AMV
                    .transform(lambda group_series: group_series - np.min(group_series)))
>>> df
   count_id   AMV  Hour  AMV_norm
0    16012E  4004    14         0
1    16012E  4026    12        22
2    16012E  4099    15        95
3    16012E  4167    11       163
4    16012E  4239    10       235
5    16012E  4324    13       320
6    16012E  4941    16       937
7    16012E  5088    17      1084
8    16012E  5283     9      1279
9    16012E  5620     8      1616
10   16012E  5946    18      1942
11   16012E  6146     7      2142
12   16012W  3622    10         0
13   16012W  3904    12       282
14   16012W  3979    11       357
15   16012W  4076     9       454
16   16012W  4189    13       567
17   16012W  4870    14      1248
18   16012W  4899    18      1277
19   16012W  5107    15      1485
20   16012W  5659     8      2037
21   16012W  6325     7      2703
22   16012W  6460    17      2838
23   16012W  6500    16      2878

编辑:@AndyHayden的方法稍微快一点:

%timeit df["AMV"] - df.groupby('count_id')["AMV"].transform('min')
1000 loops, best of 3: 736 µs per loop
%timeit df.groupby('count_id').AMV.transform(lambda x: x - np.min(x))
1000 loops, best of 3: 804 µs per loop
%timeit df.groupby('count_id').AMV.apply(lambda x: x - np.min(x))
1000 loops, best of 3: 1.32 ms per loop