如何通过在 python 上的 table 中取其他两行的百分比来创建新行

How to make a new row by taking the percentage of two other rows in a table on python

所以我有一个 pandas 数据框,显示不同坐标下的曲棍球比赛列表的投篮次数和进球数。数据框列出了像这样的射门和进球 (4, 2),我想添加另一列,将进球数除以射门数,以给出每个坐标的射门百分比。到目前为止,这是我的代码...

key in contents['liveData']['plays']['allPlays']:
        # for plays in key['result']['event']:
            # print(key)
        if (key['result']['event'] == "Shot"):
            #print(key['result']['event'])
            scoordinates = (key['coordinates']['x'], key['coordinates']['y'])
            if scoordinates not in shots:
                shots[scoordinates] = (1, 0)
            else:
                shots[scoordinates] = tuple(map(sum, zip((1, 0), shots[scoordinates])))
        if (key['result']['event'] == "Goal"):
            #print(key['result']['event'])
            gcoordinates = (key['coordinates']['x'], key['coordinates']['y'])
            if gcoordinates not in shots:
                shots[gcoordinates] = (1, 1)
            else:
                shots[gcoordinates] = tuple(map(sum, zip((1, 1), shots[gcoordinates])))
  
#create data frame using pandas
pd.set_option("display.max_rows", None, "display.max_columns", None)
sdf = pd.DataFrame(list(shots.items()),columns = ['Coordinates','Occurences (S, G)'])
file.write(f"{sdf}\n")

这给出了结果数据框 this--

    Coordinates Occurences (S, G)
0      (78.0, -19.0)            (2, 1)
1     (-37.0, -10.0)            (2, 0)
2      (47.0, -23.0)            (3, 1)
3       (53.0, 14.0)            (1, 0)
4       (77.0, -2.0)            (8, 4)
5        (80.0, 1.0)           (12, 5)
6       (74.0, 14.0)            (7, 0)
7       (87.0, -3.0)            (1, 1)

如果有人能提供帮助那就太好了!

试试这个:

df['new_col']=df['old_col'].apply( lambda x: x[1]/x[0])

只需划分 2 列。这是“更长”的方式。将 S、G 元组拆分为各自的列,然后划分。或者使用 Ave799 提供的 lambda 执行 one-liner。两者都有效,但 Ave799 可能是首选方式

import pandas as pd

data = pd.DataFrame([[(78.0, -19.0),(2, 1)],
[(-37.0, -10.0),(2, 0)],
[(47.0, -23.0),(3, 1)],
[(53.0, 14.0),(1, 0)],
[(77.0, -2.0),(8, 4)],
[(80.0, 1.0),(12, 5)],
[(74.0, 14.0),(7, 0)],
[(87.0, -3.0),(1, 1)]], columns=['Coordinates','Occurences (S, G)'])

 
data[['S','G']] = pd.DataFrame(data['Occurences (S, G)'].tolist(), index=data.index)   
data['Percentage'] = data['G'] / data['S']

输出:

print(data)
      Coordinates Occurences (S, G)  Percentage   S  G
0   (78.0, -19.0)            (2, 1)    0.500000   2  1
1  (-37.0, -10.0)            (2, 0)    0.000000   2  0
2   (47.0, -23.0)            (3, 1)    0.333333   3  1
3    (53.0, 14.0)            (1, 0)    0.000000   1  0
4    (77.0, -2.0)            (8, 4)    0.500000   8  4
5     (80.0, 1.0)           (12, 5)    0.416667  12  5
6    (74.0, 14.0)            (7, 0)    0.000000   7  0
7    (87.0, -3.0)            (1, 1)    1.000000   1  1