python - Pandas :根据条件更改单元格值

标签 python pandas

我有以下 Pandas 数据框。

import pandas as pd

data = {'id_a': [1, 1, 1, 2, 2, 2, 3, 4], 'name_a': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'd'], 
        'id_b': [5, 6, 7, 8, 9, 10, 11, 11], 'name_b': ['e', 'f', 'g', 'h', 'i', 'j', 'k', 'k'], 
        'similar': [1, 1, 1, 1, 1, 0, 1, 1], 'metric': [.5, 1, .8, .7, .2, .9, .8, .9]}
df = pd.DataFrame(data)
print(df)

      id_a   name_a   id_b   name_b   similar   metric  
 --- ------ -------- ------ -------- --------- -------- 
  0    1       a       5       e         1       0.5    
  1    1       a       6       f         1       1.0    
  2    1       a       7       g         1       0.8    
  3    2       b       8       h         1       0.7    
  4    2       b       9       i         1       0.2    
  5    2       b       10      j         0       0.9    
  6    3       c       11      k         1       0.8    
  7    4       d       11      k         1       0.9   

在此表中,组 A 的 ID 链接到组 B 的 ID(基于列 similar)。

但我需要每个组的唯一 ID 来对应另一个组的一个 ID。

并且在每组ID相同的行中,我需要选择metric列最大的行。

例如,我有三行 id_a == 2。在这三行中,只有两列的 similar 值等于 1。 在这两行中,第一行的列metric值为0.7,第二行的值为0.2。

我将列 similar 的值保留为 1,仅用于列 metric 为 0.7 的行(因为它是最大值),而对于第二行 I将列 similar 的值设为 0。

也就是说,我需要以下数据框:

output_data = {'id_a': [1, 1, 1, 2, 2, 2, 3, 4], 'name_a': ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'd'], 
               'id_b': [5, 6, 7, 8, 9, 10, 11, 11], 'name_b': ['e', 'f', 'g', 'h', 'i', 'j', 'k', 'k'], 
               'similar': [0, 1, 0, 1, 0, 0, 0, 1], 'metric': [.5, 1, .8, .7, .2, .9, .8, .9]}
output_df = pd.DataFrame(output_data)
print(output_df)

      id_a   name_a   id_b   name_b   similar   metric  
 --- ------ -------- ------ -------- --------- -------- 
  0    1       a       5       e         0       0.5    
  1    1       a       6       f         1       1.0    
  2    1       a       7       g         0       0.8    
  3    2       b       8       h         1       0.7    
  4    2       b       9       i         0       0.2    
  5    2       b       10      j         0       0.9    
  6    3       c       11      k         0       0.8    
  7    4       d       11      k         1       0.9    

问题:如何使用 Python 来实现(因为我的研究没有给出任何结果)?

最佳答案

使用 groupby idxmaxisin 和 listcomp 中的 2 个 groupby 并传递给 np.array。最后,在 np.array

上调用 allastype
df1 = df[df.similar.eq(1)]
df['similar'] = np.array([df.index.isin(df1.groupby(col).metric.idxmax()) 
                            for col in ['id_a', 'id_b']]).all(0).astype(int)


Out[132]:
   id_a name_a  id_b name_b  similar  metric
0     1      a     5      e        0     0.5
1     1      a     6      f        1     1.0
2     1      a     7      g        0     0.8
3     2      b     8      h        1     0.7
4     2      b     9      i        0     0.2
5     2      b    10      j        0     0.9
6     3      c    11      k        0     0.8
7     4      d    11      k        1     0.9

关于python - Pandas :根据条件更改单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58942290/

相关文章:

python - 获取列名称列表的数据框

python - 使用 Matplotlib 格式化绘图

python - 需要帮助构建 SQLAlchemy 查询 + 子查询

python - 比较Python时间和MySQL时间格式

python - 循环遍历 pandas DataFrame 时出现意外结果

sql - 使用对列表有效地选择 SQL Server 中的行?

python - 如何从数据框中创建一个列表,其中包含字符串列的每个值乘以第二列?

Python BeautifulSoup 只是读取第一行

python - 使用大于 24 小时的时间值

python - 如何通过从另一列复制值来填充缺失的 DataFrame 值