首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据另一个表的检查替换熊猫数据中列中的值

如何根据另一个表的检查替换熊猫数据中列中的值
EN

Stack Overflow用户
提问于 2020-12-16 15:52:12
回答 2查看 52关注 0票数 0

我有一个数据文件df

代码语言:javascript
复制
import pandas as pd
df = pd.DataFrame({"Cust": ['cst1', 'cst1', 'cst1', 'cst2', 'cst2', 'cst2', 'cst3', 'cst3', 'cst3', 'cst4', 'cst4', 'cst4'],
                   "act": ['ac1', 'ac2', 'ac3','ac1', 'ac2', 'ac3','ac1', 'ac2', 'ac3','ac1', 'ac2', 'ac3' ],
                   "rating": ['a', 'b', 'c', 'b', 'b', 'c', 'h', 'i', 'i', 'c', 'c', 'a']})

df_priority = pd.DataFrame({"rating":['a','b', 'c', 'd', 'e', 'f','g','h','i','j','k','l','m','n','o','p','q','r','s'],
                            "priority":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]})

和另一个具有优先级 of with 的数据

我的df表如下所示:

代码语言:javascript
复制
    Cust  act rating
0   cst1  ac1      a
1   cst1  ac2      b
2   cst1  ac3      c
3   cst2  ac1      b
4   cst2  ac2      b
5   cst2  ac3      c
6   cst3  ac1      h
7   cst3  ac2      i
8   cst3  ac3      i
9   cst4  ac1      c
10  cst4  ac2      c
11  cst4  ac3      a

我的df_priority表看起来像:

代码语言:javascript
复制
   rating  priority
0       a         1
1       b         2
2       c         3
3       d         4
4       e         5
5       f         6
6       g         7
7       h         8
8       i         9
9       j        10
10      k        11
11      l        12
12      m        13
13      n        14
14      o        15
15      p        16
16      q        17
17      r        18
18      s        19

我需要检查并将每个 cust df表中的评级值替换为该的最高优先级等级。

例如,对于cust = cst1,对于所有三个记录,我应该将其评级为a,因为a的优先级大于b和c。类似地,它应该针对每个cust,然后检查pririty表并相应更新。

我的预期产出是:

代码语言:javascript
复制
    Cust  act rating
0   cst1  ac1      a
1   cst1  ac2      a
2   cst1  ac3      a
3   cst2  ac1      b
4   cst2  ac2      b
5   cst2  ac3      b
6   cst3  ac1      h
7   cst3  ac2      h
8   cst3  ac3      h
9   cst4  ac1      a
10  cst4  ac2      a
11  cst4  ac3      a

我怎么才能在潘达斯做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-16 15:59:17

让我们尝试将评级映射到它的优先级,然后使用groupbyidxmin找到最高优先级的优先级,最后重新分配:

代码语言:javascript
复制
idx=(df['rating'].map(df_priority.set_index('rating')['priority'])
   .groupby(df['Cust']).transform('idxmin')
)
df['rating'] = df.loc[idx,'rating'].values

输出:

代码语言:javascript
复制
    Cust  act rating
0   cst1  ac1      a
1   cst1  ac2      a
2   cst1  ac3      a
3   cst2  ac1      b
4   cst2  ac2      b
5   cst2  ac3      b
6   cst3  ac1      h
7   cst3  ac2      h
8   cst3  ac3      h
9   cst4  ac1      a
10  cst4  ac2      a
11  cst4  ac3      a
票数 1
EN

Stack Overflow用户

发布于 2020-12-16 15:59:09

我们可以使用transform idxmim执行transform,然后用reindex重新分配它。

代码语言:javascript
复制
df['new']=df.rating.map(dict(zip(df_priority.rating,df_priority.priority)))
df.groupby('Cust').new.transform('idxmin')
0      0
1      0
2      0
3      3
4      3
5      3
6      6
7      6
8      6
9     11
10    11
11    11
Name: new, dtype: int64

df['newcol'] = df.rating.reindex(df.groupby('Cust').new.transform('idxmin')).tolist()
df
    Cust  act rating  new newcol
0   cst1  ac1      a    1      a
1   cst1  ac2      b    2      a
2   cst1  ac3      c    3      a
3   cst2  ac1      b    2      b
4   cst2  ac2      b    2      b
5   cst2  ac3      c    3      b
6   cst3  ac1      h    8      h
7   cst3  ac2      i    9      h
8   cst3  ac3      i    9      h
9   cst4  ac1      c    3      a
10  cst4  ac2      c    3      a
11  cst4  ac3      a    1      a
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65326674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档