Python 熊猫-基于现有列值填充新列

Python 熊猫-基于现有列值填充新列,python,pandas,Python,Pandas,我有以下数据帧df_shots: TableIndex MatchID GameWeek Player ... ShotPosition ShotSide Close Position ShotsDetailID ...

我有以下数据帧
df_shots

              TableIndex  MatchID  GameWeek           Player  ...      ShotPosition    ShotSide      Close             Position
ShotsDetailID                                                 ...                                                              
6                      5    46605         1  Roberto Firmino  ...  very close range         N/A      close  very close rangeN/A
8                      7    46605         1  Roberto Firmino  ...           the box  the centre  not close    the boxthe centre
10                     9    46605         1  Roberto Firmino  ...           the box    the left  not close      the boxthe left
17                    16    46605         1  Roberto Firmino  ...           the box  the centre      close    the boxthe centre
447                  446    46623         2  Roberto Firmino  ...           the box  the centre      close    the boxthe centre
...                  ...      ...       ...              ...  ...               ...         ...        ...                  ...
6656                6662    46870        27  Roberto Firmino  ...  very close range         N/A      close  very close rangeN/A
6666                6672    46870        27  Roberto Firmino  ...           the box   the right  not close     the boxthe right
6674                6680    46870        27  Roberto Firmino  ...           the box  the centre  not close    the boxthe centre
6676                6682    46870        27  Roberto Firmino  ...           the box    the left  not close      the boxthe left
6679                6685    46870        27  Roberto Firmino  ...   outside the box         N/A  not close   outside the boxN/A
为清楚起见,所有可能的“位置”值为:

positions = ['a difficult anglethe left',
             'a difficult anglethe right',
             'long rangeN/A',
             'long rangethe centre',
             'long rangethe left',
             'long rangethe right',
             'outside the boxN/A',
             'penaltyN/A',
             'the boxthe centre',
             'the boxthe left',
             'the boxthe right',
             'the six yard boxthe left',
             'the six yard boxthe right',
             'very close rangeN/A']
现在,我想将以下x/y值映射到每个“位置”名称,并将该值存储在新的“位置XY”列下:

    the_boxthe_center = {'y':random.randrange(25,45), 'x':random.randrange(0,6)}
    the_boxthe_left = {'y':random.randrange(41,54), 'x':random.randrange(0,16)}
    the_boxthe_right = {'y':random.randrange(14,22), 'x':random.randrange(0,16)}
    very_close_rangeNA = {'y':random.randrange(25,43), 'x':random.randrange(0,4)}
    six_yard_boxthe_left = {'y':random.randrange(33,43), 'x':random.randrange(4,6)}
    six_yard_boxthe_right = {'y':random.randrange(25,33), 'x':random.randrange(4,6)}
    a_diffcult_anglethe_left = {'y':random.randrange(43,54), 'x':random.randrange(0,6)}
    a_diffcult_anglethe_right = {'y':random.randrange(14,25), 'x':random.randrange(0,6)}
    penaltyNA = {'y':random.randrange(36), 'x':random.randrange(8)}
    outside_the_boxNA = {'y':random.randrange(14,54), 'x':random.randrange(16,28)}
    long_rangeNA = {'y':random.randrange(0,68), 'x':random.randrange(40,52)}
    long_rangethe_centre = {'y':random.randrange(0,68), 'x':random.randrange(28,40)}
    long_rangethe_right = {'y':random.randrange(0,14), 'x':random.randrange(0,24)}
    long_rangethe_left = {'y':random.randrange(54,68), 'x':random.randrange(0,24)}

我试过:

if df_shots['Position']=='very close rangeN/A':
        df_shots['Position X/Y']==very_close_rangeNA
...# and so on
但我得到:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


我如何做到这一点?

这里有一些代码可能会达到您想要的效果

首先创建一个列表,列出你所有的“位置XY”,如

以及相应的
位置
列表(如您已经拥有的) 然后我建议你做一个字典,这样每个位置都会进行相应的位置xy计算

dict_positionxy = dict(zip(position, position_xy))
然后在数据框中创建一个新列,在其中根据位置存储x,y值

 df_shots['Position X/Y'] = 0.
现在一行一行地遍历所有行

for index, row in df_shots.iterrows():
    for key, values in dict_positionxy.items():

       if row['Position'] == key:
           #row['Position X/Y'] = value
           df_shots.at[index,’Position X/Y’]= value

print(df_shots)

这应该可以做到这一点:)

这里有一些示例代码,可以实现您想要的功能。我创建了一个基本的df_快照模型,但是在更大的数据帧上应该运行相同的模型。我还将一些自由变量存储在
dict
中,以简化过滤

需要注意的是,由于预先计算了
位置的随机值,因此每个放炮位置的所有x/y值都是相同的。这可能不是你想要的

将熊猫作为pd导入
随机输入
#测向镜头样本
df_shots=pd.DataFrame({'Position':['the_boxthe_center','the_boxthe_left']})
#在dict中存储位置/xy对
位置_xy={'the_box the_center':{'y':random.randrange(25,45),'x':random.randrange(0,6)},
'the_boxthe_left':{'y':random.randrange(41,54),'x':random.randrange(0,16)}
#创建新列
df_放炮['Position XY']='
#迭代所有位置/xy对
对于位置,xy在位置_xy.items()中:
#确定所有匹配玩家的指数
匹配=df_放炮['Position']==位置
matches\u index=匹配[matches].index
#使用xy更新df_快照中的匹配行
对于匹配索引中的idx:
[idx'位置XY']=XY处的df_放炮
打印(df_快照)
产出:

            Position        Position XY
0  the_boxthe_center  {'y': 36, 'x': 2}
1    the_boxthe_left  {'y': 44, 'x': 0}

在一个容器外存储这么多相关变量是一种糟糕的形式,让我们使用映射到数据帧的字典

data_dict = 
{'the boxthe centre': {'y':random.randrange(25,45)...}


df['Position'] = df['Position'].map(data_dict)

print(df['Position'])
6        {'y': 35, 'x': 2}
8        {'y': 32, 'x': 1}
10      {'y': 44, 'x': 11}
17       {'y': 32, 'x': 1}
447      {'y': 32, 'x': 1}
...                    NaN
6656     {'y': 35, 'x': 2}
6666    {'y': 15, 'x': 11}
6674     {'y': 32, 'x': 1}
6676    {'y': 44, 'x': 11}
6679    {'y': 37, 'x': 16}
Name: Position, dtype: object

嘿,伙计,你不能将一个序列与一个字符串进行比较(df_shots['Position']是你的数据帧的一列,也可以被视为一个序列),因此你会收到这个错误消息。此外,出于同样的原因,与非常接近的rangeNA进行比较也不起作用。。您需要循环,如果您想要循环所有行,您可以简单地使用.iterrows()方法作为旁白,所有这些松散变量可能都属于某种类型的数据结构。这将减少生成所有这些xy坐标点时的大量键入,通常会使工作更愉快。哦,谢谢,我如何修复它?您确定iterrows部分是正确的吗?我得到的所有“位置X/Y”值都是0.0。@8位Borges我更正了代码,因为您正在使用itterows循环,引用row不会更新其值。使用at方法将更新值,请立即尝试!我得到了
ValueError:使用iterable设置时,len键和值必须相等。有什么想法吗?你能分享整个轨迹吗?这样我就能知道是哪条线导致了这个错误?
data_dict = 
{'the boxthe centre': {'y':random.randrange(25,45)...}


df['Position'] = df['Position'].map(data_dict)

print(df['Position'])
6        {'y': 35, 'x': 2}
8        {'y': 32, 'x': 1}
10      {'y': 44, 'x': 11}
17       {'y': 32, 'x': 1}
447      {'y': 32, 'x': 1}
...                    NaN
6656     {'y': 35, 'x': 2}
6666    {'y': 15, 'x': 11}
6674     {'y': 32, 'x': 1}
6676    {'y': 44, 'x': 11}
6679    {'y': 37, 'x': 16}
Name: Position, dtype: object