Skip to main content
 首页 » 编程设计

python之如何根据另一列找到一行的值

2025年01月19日9xing901022

我有这样一个数据集:

user-id     time    location   msg  path 
  1           1         1       1    0 
  2           1         1       2    1000 
  3           1         2       3    1 
  4           1         2       0    0 
  5           1         3       0    0 
  1           2         2       2    0 
  2           2         1       1    1 
  3           2         1       1    1 
  4           2         0       0    0 
  5           2         0       0    0 
  1           3         1       3    0 
  2           3         3       1    0 

我想根据最大消息数找到两条记录的时间和位置相同的路径。

time_locs = pd.unique(df['time_loc']) 
for time_loc in time_locs: 
   dc_group = df[df['time_loc'] == time_loc] 
   if(len(dc_group) > 1): 
        max_num_msg = max(dc_group['msgs']) 

所以我将时间和位置压缩为 time_loc 并找到了最大消息数。现在我怎样才能找到那一行的路径?

例如,在这种情况下,我的第一个 dc-group 是这两行:

user-id     time    location   msg  path 
  1           1         1       1    0 
  2           1         1       2    1000 

我想找到 1000 个。

我试过这段代码,但它不起作用。

user_group = df.loc[max(dc_group['msgs']), 'path'] 

因为是在所有df中搜索。并且 .loc 不适用于 dc_group,这意味着此代码面临错误:

user_group = dc_group.loc[max(dc_group['msgs']), 'path'] 

请您参考如下方法:

您肯定想在这里使用非循环方法。您可以使用 .argmax 获取最大值的索引而不是值本身。像这样的东西:

In [15]: df 
Out[15]: 
    user-id  time  location  msg  path 
0         1     1         1    1     0 
1         2     1         1    1     0 
2         3     1         2    0     0 
3         4     1         2    0     0 
4         5     1         3    0     0 
5         1     2         2    2     0 
6         2     2         1    1     0 
7         3     2         1    1     0 
8         4     2         0    0     0 
9         5     2         0    0     0 
10        1     3         1    3     0 
11        2     3         3    1     0 
 
In [16]: df.loc[df.time == df.location, 'msg'].argmax() 
Out[16]: 5 
 
In [17]: max_idx = df.loc[df.time == df.location, 'msg'].argmax() 
 
In [18]: df.loc[max_idx] 
Out[18]: 
user-id     1 
time        2 
location    2 
msg         2 
path        0 
Name: 5, dtype: int64 
 
In [19]: df.loc[max_idx, 'path'] 
Out[19]: 0 

如果你想要所有的行,那么就使用 bool 索引:

In [25]: df.loc[df.time == df.location] 
Out[25]: 
    user-id  time  location  msg  path 
0         1     1         1    1     0 
1         2     1         1    1     0 
5         1     2         2    2     0 
11        2     3         3    1     0 

或者 .query 如果你愿意的话:

In [26]: df.query('time == location') 
Out[26]: 
    user-id  time  location  msg  path 
0         1     1         1    1     0 
1         2     1         1    1     0 
5         1     2         2    2     0 
11        2     3         3    1     0