简体   繁体   English

Python Pandas - 计算到目前为止在每一行的列中找到的字符串

[英]Python Pandas - count string found in column so far for every row

I am doing some analysis and visuals on past NHL seasons and I'm having difficulty in creating some additional data/features in my DataFrame.我正在对过去的 NHL 赛季进行一些分析和视觉效果,但在我的 DataFrame 中创建一些额外的数据/功能时遇到了困难。

This is a simplified version of the dataframe where each row represents one game.这是数据框的简化版本,其中每一行代表一个游戏。

game_id h_abbr a_abbr 
0001    WSH    TOR 
0002    ANA    TOR 
0003    TOR    MIN 

How can I count how many games each team has played so far (including the game in question) so the new column would look like something like this?我如何计算到目前为止每支球队打了多少场比赛(包括有问题的比赛),以便新专栏看起来像这样?

game_id h_abbr a_abbr ht_game_no at_game_no
0001    WSH    TOR    1          1
0002    ANA    TOR    1          2
0003    TOR    MIN    3          1

After a lot of searching and trying I have only found a way to count how many home OR away games the team has played, but I am interested in the total games played.经过大量的搜索和尝试,我只找到了一种方法来计算球队打了多少场主场或客场比赛,但我对比赛总数感兴趣。

df['Nth_away_game'] = df.groupby('a_abbr').cumcount() + 1

Idea is reshape values by DataFrame.stack , then use GroupBy.cumcount , check columns names by DataFrame.add_suffix and last DataFrame.join to original:想法是通过DataFrame.stack重塑值,然后使用GroupBy.cumcount ,通过DataFrame.add_suffix检查列名称,最后DataFrame.join到原始:

s = df[['h_abbr','a_abbr']].stack()
df = df.join(s.groupby(s).cumcount().add(1).unstack().add_suffix('_no'))
print (df)
   game_id h_abbr a_abbr  h_abbr_no  a_abbr_no
0        1    WSH    TOR          1          1
1        2    ANA    TOR          1          2
2        3    TOR    MIN          3          1

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

相关问题 Python 计数 pandas 在 pandas 中 substring 的出现次数 - Python Count occurrences of a substring in pandas by row appending distinct string as column 用熊猫计算列中每行的字符串数 - count number of string per row in a column with pandas 用到目前为止已出现的次数替换每次出现的字符串 - Replace every occurrence of a string with the number of times it has occurred so far Pandas/ Python - 每 X 行将一列转换为新列 - Pandas/ Python - Convert one column into new column every X row python pandas:如何对列中的每个值进行分组并计算条件? - python pandas: how to group by and count with a condition for every value in a column? 对于 pandas dataframe 中的每一行,检查列是否包含最后 5 行中的字符串 - For every row in a pandas dataframe, check if a column contains a string in in the last 5 rows 如何将字符串添加到pandas dataframe列系列中的每个偶数行? - How to add a string to every even row in a pandas dataframe column series? 将每隔一行移动到一个新列并将 Pandas python 分组 - Moving every other row to a new column and group pandas python 如何使用 Pandas/Python 在特定列中的每个项目行上使用 function - How to use function on every item row in specific column with Pandas/Python 两个DataFrames(Python / Pandas)中的每一行和每一列之间的差异 - Difference between every row and column in two DataFrames (Python / Pandas)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM