繁体   English   中英

如何从字符串的特定值排序?

[英]How to sort from specific values of string?

我正在使用 pandas,我的数据框是这样的:

Unnamed: 0    id             player_name  games  time  goals         xG  \
0             0   647              Harry Kane     35  3097     23  22.174859   
1             1  1250           Mohamed Salah     37  3085     22  20.250847   
2             2  1228         Bruno Fernandes     37  3117     18  16.019454   
3             3   453           Son Heung-Min     37  3139     17  11.023287   
4             4   822         Patrick Bamford     38  3085     17  18.401863   
..          ...   ...                     ...    ...   ...    ...        ...  

  assists         xA  shots  key_passes  yellow_cards  red_cards position  \
0         14   7.577094    138          49             1          0        F   
1          5   6.528526    126          55             0          0    F M S   
2         12  11.474996    121          95             6          0      M S   
3         10   9.512992     68          75             0          0    F M S   
4          7   3.782247    107          30             3          0      F S   
..       ...        ...    ...         ...           ...        ...      ...  

       team_title  npg       npxG    xGChain  xGBuildup  
0            Tottenham   19  19.130183  24.995648   4.451257  
1            Liverpool   16  15.683834  28.968234   9.800236  
2    Manchester United    9   8.407840  26.911412  11.932285  
3            Tottenham   16  10.262118  20.671916   6.608751  
4                Leeds   15  16.879525  23.394953   4.131796  
..                 ...  ...        ...        ...        ...  

我正在尝试按team_title对其进行分组,并按goalsassists对其进行排序,这将变成这样:

team_title  player_name  goals  assists
Tottenham    Harry Kane  23      14
Tottenham    Gareth Bale ...     ...
Tottenham    Son Heung-Min ...   ...

我试过使用

if any('Tottenham' in db['team_title']):
    db.groupby('team_title')['player_name',''goals','assists'].value_counts()

我得到的错误信息是

TypeError: 'bool' object 不可迭代

使用if-else是否正确,或者是否有任何其他方法可以从字符串的特定值进行排序?

如果我理解你想正确做什么,你想按团队名称对 DataFrame 中的sort_values()进行排序(因此将它们分组在一起),然后按目标和助攻(按此顺序)排序,最高的在前。

你可以用这行代码来做到这一点:

db.sort_values(by=["team_title", "goals", "assists"], ascending=[True, False, False], inplace=True)

其使用示例:

import pandas as pd
db = pd.DataFrame(data={"id": [0, 1, 2, 3, 4, 5], "player_name": ["Kane", "Salag", "Fernandes", "Heung-Min", "Bamford", "PlayerX"],
                        "games": [35, 37, 37, 37, 38, 35], "time": [3097, 2085, 3117, 3139, 3085, 4000],
                        "goals": [23, 22, 18, 17, 17, 12], "assists": [14, 5, 12 ,10, 7, 9],
                        "team_title": ["Tottenham", "Liverpool", "Manchester United", "Tottenham", "Leeds", "Tottenham"]})

db.sort_values(by=["team_title", "goals", "assists"], ascending=[True, False, False], inplace=True)

通过给升序列表,你可以给你排序的每个项目的方向,所以在这个例子中:

  1. 按 team_title 升序排序(这会将所有团队组合在一起)。
  2. 按进球数降序排列(最高的在前)。
  3. 最后,按助攻数降序排列。

Output 示例数据:

#Out: 
#   id player_name  games  time  goals  assists         team_title
#4   4     Bamford     38  3085     17        7              Leeds
#1   1       Salag     37  2085     22        5          Liverpool
#2   2   Fernandes     37  3117     18       12  Manchester United
#0   0        Kane     35  3097     23       14          Tottenham
#3   3   Heung-Min     37  3139     17       10          Tottenham
#5   5     PlayerX     35  4000     12        9          Tottenham

如果您随后想要专门检索“Tottenham”数据:

print(db[db["team_title"] == "Tottenham"])
#Out: 
#   id player_name  games  time  goals  assists team_title
#0   0        Kane     35  3097     23       14  Tottenham
#3   3   Heung-Min     37  3139     17       10  Tottenham
#5   5     PlayerX     35  4000     12        9  Tottenham

您的 if 语句不起作用,因为您正在有效地编写if any(True): ,但any()不接受 bool 作为输入。 请参阅此GeeksforGeeks 页面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM