如何在python上通过获取表中其他两行的百分比生成新行

如何在python上通过获取表中其他两行的百分比生成新行,python,pandas,dataframe,dictionary,frame,Python,Pandas,Dataframe,Dictionary,Frame,所以我有一个熊猫数据框,它显示了从不同坐标系的曲棍球比赛列表中的投篮次数和进球次数。数据框列出了这样的快照和目标(4,2),我想添加另一列,将目标除以快照,以给出每个坐标的快照百分比。到目前为止,这是我的代码 key in contents['liveData']['plays']['allPlays']: # for plays in key['result']['event']: # print(key) if (key['result

所以我有一个熊猫数据框,它显示了从不同坐标系的曲棍球比赛列表中的投篮次数和进球次数。数据框列出了这样的快照和目标(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")
这将给出此帧的结果数据--

如果有人能帮忙,那就太好了

试试这个:

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

把这两列分开。这是“更长”的方法。将S、G元组拆分为各自的列,然后进行拆分。或者使用Ave799提供的lambda做一个衬里。这两种方法都有效,但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
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