基于DataFram中多索引的一级值替换值

2024-06-28 21:36:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有多索引的数据帧。级别为gendertype和最后age。我想用一个年龄组中的另一个年龄组替换这个年龄组的值。所以我猜我需要使用.groupby()

下面我给出了一个我遇到的问题的例子

这是我最初使用的数据帧:

Index    Gender    Type    Age    Value
0        'f'       'a'     0      'A1'
1        'f'       'a'     1      'A2'
2        'f'       'a'     2      'B1'
3        'f'       'a'     3      'xx'
4        'f'       'a'     4      'B5'
5        'f'       'a'     5      'F3'
6        'f'       'a'     6      'B6'
7        'f'       'a'     7      'Q10'
8        'f'       'a'     8      'A3'
9        'f'       'a'     9      'A1'
10       'f'       'b'     0      'D1'
11       'f'       'b'     1      'V2'
12       'f'       'b'     2      'V1'
13       'f'       'b'     3      'xx'
14       'f'       'b'     4      'G5'
15       'f'       'b'     5      'D3'
16       'f'       'b'     6      'B6'
17       'f'       'b'     7      'Q14'
18       'f'       'b'     8      'A3'
19       'm'       'a'     0      'A1'
20       'm'       'a'     1      'A2'
21       'm'       'a'     2      'B1'
21       'm'       'a'     3      'xx'
23       'm'       'a'     4      'B5'
24       'm'       'a'     5      'A3'
25       'm'       'a'     6      'B6'
26       'm'       'a'     7      'B15'
27       'm'       'a'     8      'A3'
28       'm'       'a'     9      'A1'
29       'm'       'b'     2      'V1'
30       'm'       'b'     3      'xx'
31       'm'       'b'     4      'R5'
32       'm'       'b'     5      'B3'
33       'm'       'b'     6      'W6'
34       'm'       'b'     7      'Q12'

可见age==3的每一行的值都是xx。 我希望在每个性别类型组中,将该值替换为7岁的值

即:

Index    Gender    Type    Age    Value
0        'f'       'a'     0      'A1'
1        'f'       'a'     1      'A2'
2        'f'       'a'     2      'B1'
3        'f'       'a'     3      'Q10'
4        'f'       'a'     4      'B5'
5        'f'       'a'     5      'F3'
6        'f'       'a'     6      'B6'
7        'f'       'a'     7      'Q10'
8        'f'       'a'     8      'A3'
9        'f'       'a'     9      'A1'
10       'f'       'b'     0      'D1'
11       'f'       'b'     1      'V2'
12       'f'       'b'     2      'V1'
13       'f'       'b'     3      'Q14'
14       'f'       'b'     4      'G5'
15       'f'       'b'     5      'D3'
16       'f'       'b'     6      'B6'
17       'f'       'b'     7      'Q14'
18       'f'       'b'     8      'A3'
19       'm'       'a'     0      'A1'
20       'm'       'a'     1      'A2'
21       'm'       'a'     2      'B1'
21       'm'       'a'     3      'B15'
23       'm'       'a'     4      'B5'
24       'm'       'a'     5      'A3'
25       'm'       'a'     6      'B6'
26       'm'       'a'     7      'B15'
27       'm'       'a'     8      'A3'
28       'm'       'a'     9      'A1'
29       'm'       'b'     2      'V1'
30       'm'       'b'     3      'Q12'
31       'm'       'b'     4      'R5'
32       'm'       'b'     5      'B3'
33       'm'       'b'     6      'W6'
34       'm'       'b'     7      'Q12'

请注意,数据框是不平衡的,因为每个性别类型组中的年龄范围不相同。它不是在同一个年龄开始和结束的,因此年龄3在每个组中不是相同的索引,我不能使用iloc,而是以某种方式使用loc

事先谢谢你的帮助


Tags: 数据a2a1a3b1v1xx年龄
1条回答
网友
1楼 · 发布于 2024-06-28 21:36:11

您可以定义将单独处理每个组的自定义函数:

def fix(g):
    g.loc[g['Age'] == 3, 'Value'] = g.loc[g['Age'] == 7, 'Value'].iloc[0]
    return g

df.groupby(['Gender', 'Type']).apply(fix)

相关问题 更多 >