如何在 Pandas 数据框中按两列进行分组和映射

5

我在使用Python的Pandas DataFrame时遇到了问题,我正在尝试创建一个机器学习模型来预测表面。在训练数据框中,我有表面列,但在测试数据框中没有。因此,我想基于训练数据框中的表面创建一些特征,例如:

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

我已经将“cat”特征的分组值设置为表面积的平均值。很酷。

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

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)的行上。

希望你能理解我的意思。


你可以用示例数据编写简单的代码,这样每个人都能运行它并创建解决方案。 - furas
1个回答

3

您可以使用以下方法进行翻译:

  • groupby().means() 方法可以计算平均值。
  • reset_index() 方法可以将索引 Cat1, Cat2 转换回列。
  • merge(how='left') 方法可以像在数据库中的表格一样连接两个数据框(在 SQL 中为 LEFT JOIN )。
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

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