如何通过两列PANDAS DATAFRAME分组和映射



我在python上使用PANDAS DataFrame有一个问题,我正在尝试使机器学习模型预测表面。我在火车数据框架中具有表面列,并且在测试数据框架中没有它。因此,我将根据火车中的表面创建一些功能。

train['error_cat1'] = abs(train.groupby(train['cat1'])['surface'].transform('mean')  - train.surface.mean())

在这里,我将" CAT"功能的Grouby的值设置为Suface的平均值。酷

现在我也必须将其添加到测试中。因此,将使用此方法将每个组的火车的值映射到测试行。

mp = {k: g['error_cat1'].tolist()[0] for k,g in train.groupby('cat1')}
test['error_cat1'] = test['cat1'].map(mp)

所以,远没有问题。现在,我将在Groupby中使用两列。

train['error_cat1_cat2'] = abs(train.groupby(train[['cat1','cat2']])['surface'].transform('mean')  - train.surface.mean())

,但我不知道如何将其映射以进行测试框架。请您可以帮助我处理这个问题或给我一些其他方法,以便我可以做到。

谢谢

例如,我的火车是

+------+------+-------+
| Cat1 | Cat2 | surface |
+------+------+-------+
| 1    | 3    | 10    |
+------+------+-------+
| 2    | 2    | 12    |
+------+------+-------+
| 3    | 1    | 12    |
+------+------+-------+
| 1    | 3    | 5     |
+------+------+-------+
| 2    | 2    | 10    |
+------+------+-------+
| 3    | 2    | 13    |
+------+------+-------+

我的测试是

+------+------+
| Cat1 | Cat2 |
+------+------+
| 1    | 2    |
+------+------+
| 2    | 1    |
+------+------+
| 3    | 1    |
+------+------+
| 1    | 3    |
+------+------+
| 2    | 3    |
+------+------+
| 3    | 1    |
+------+------+

现在,我将在CAT1和CAT2上进行组平均表面,例如(Cat1,cat2)=(1,3)为(10 5)/2 = 7.5

现在,我必须去测试并在(Cat1,cat2)=(1,3)行上映射此值。

我希望你能得到我。

您可以使用

  • groupby().means()计算均值
  • reset_index()将索引Cat1Cat2再次转换为列
  • merge(how='left', )要加入两个数据库,例如数据库中的表(LEFT JOIN中的CC_6)。

headers = ['Cat1', 'Cat2', 'surface']
train_data = [
    [1, 3, 10],
    [2, 2, 12],
    [3, 1, 12],
    [1, 3, 5],
    [2, 2, 10],
    [3, 2, 13],
]
test_data = [
    [1, 2],
    [2, 1],
    [3, 1],
    [1, 3],
    [2, 3],
    [3, 1],
]
import pandas as pd
train = pd.DataFrame(train_data, columns=headers)
test = pd.DataFrame(test_data, columns=headers[:-1])
print('--- train ---')
print(train)
print('--- test ---')
print(test)
print('--- means ---')
means = train.groupby(['Cat1', 'Cat2']).mean()
print(means)
print('--- means (dataframe) ---')
means = means.reset_index(level=['Cat1', 'Cat2'])
print(means)
print('--- result ----')
result = pd.merge(df2, means, on=['Cat1', 'Cat2'], how='left')
print(result)
print('--- result (fillna)---')
result = result.fillna(0)
print(result)

结果:

--- train ---
   Cat1  Cat2  surface
0     1     3       10
1     2     2       12
2     3     1       12
3     1     3        5
4     2     2       10
5     3     2       13
--- test ---
   Cat1  Cat2
0     1     2
1     2     1
2     3     1
3     1     3
4     2     3
5     3     1
--- means ---
           surface
Cat1 Cat2         
1    3         7.5
2    2        11.0
3    1        12.0
     2        13.0
--- means (dataframe) ---
   Cat1  Cat2  surface
0     1     3      7.5
1     2     2     11.0
2     3     1     12.0
3     3     2     13.0
--- result ----
   Cat1  Cat2  surface
0     1     2      NaN
1     2     1      NaN
2     3     1     12.0
3     1     3      7.5
4     2     3      NaN
5     3     1     12.0
--- result (fillna)---
   Cat1  Cat2  surface
0     1     2      0.0
1     2     1      0.0
2     3     1     12.0
3     1     3      7.5
4     2     3      0.0
5     3     1     12.0

最新更新