简体   繁体   English

Python / Pandas:如何在一列中选择值等于另一列中另一行的行?

[英]Python/Pandas: How do I select rows in one column where value iis equal to a different row in a different column?

Here is a sample of my data: 这是我的数据样本:

In[177]:df_data[['Date', 'TeamName', 'Opponent', 'ScoreOff']].head()
Out[177]: 
                     Date              TeamName              Opponent   ScoreOff
4128  2005-09-08 00:00:00  New England Patriots       Oakland Raiders   30
4129  2005-09-08 00:00:00       Oakland Raiders  New England Patriots   20
4130  2005-09-11 00:00:00     Arizona Cardinals       New York Giants   19
4131  2005-09-11 00:00:00      Baltimore Ravens    Indianapolis Colts   7
4132  2005-09-11 00:00:00         Buffalo Bills        Houston Texans   22

For each row, I need to set a new column ['OpponentScoreOff'] equal to that team's opponent's ScoreOff on that day. 对于每一行,我需要设置一个新列['OpponentScoreOff'],该列等于该团队当天对手的ScoreOff。

I have done it by basically doing the following, but it's slow and I feel like there is a more pythonic/vectorized way to do it. 我基本上是通过执行以下操作来完成此操作的,但是它很慢,而且我觉得还有更多的pythonic / vectorized方式可以做到。

g1 = df_data.groupby('Date')
for date, teams in g1:
    g2 = teams.groupby('TeamName')
    for teamname, game in teams:
        df_data[(df_data['TeamName'] == teamname) & (dfdata['Date'] == date)]['OppScoreOff'] =     df_data[(df_data['Opponent'] == teamname) & (df_data['Date'] == date)]['ScoreOff']

It worked, but it's slow. 它起作用了,但是很慢。 Any better way to do this? 还有更好的方法吗?

You could use sort to take advantage of the bijection between TeamName and Opponent for any given date. 您可以使用sort来利用任何给定日期的TeamName和Opponent之间的双射。 Consider the following: 考虑以下:

import pandas as pd
import numpy as np

df_data = df_data.sort(['Date', 'TeamName'])
opp_score = np.array(df_data.sort(['Date', 'Opponent'])['ScoreOff'])
df_data['OpponentScoreOff'] = opp_score

The array call is necessary to remove the DataFrame indexing. 数组调用对于删除DataFrame索引是必需的。 That way, the array isn't resorted once it's put back into df_data . 这样,一旦将数组放回df_data ,就不会再使用该数组。

暂无
暂无

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

相关问题 选择在一列中具有相同值而在另一列中具有不同值的行 Pandas Python - Select rows with equal values in one column and different values in another column Pandas Python 我如何像重复使用Python一样使用SQL,方法是在更改该行中的一个值(该值从另一列派生)的同时复制行? - How do I use SQL like I use Python by duplicating rows while changing one value in that row where that value is derived from a different column? 如何将一列中的行值与组中不同列中的所有其他行进行比较? - How do I compare a row value in one column to all other rows in a different column within a group? 如何基于另一列的值(其中两个不同的列中的值在pandas中相等)找到一列的行索引? - How to find row index of one column based on values of a different column where the values in the two distinct columns are equal in pandas? Python Pandas:如果行 [i] 上的单独列等于 col 一个值,则通过将一列值与不同的行 [i] 和列匹配来创建新列 - Python Pandas: Create new column by matching one column value to a different row [i] and column if a separate column on row [i] equals col one value 如何创建Pandas Dataframe,其中每行的列集不同? - How do I create a Pandas Dataframe where the column set is different for each row? pandas - 根据不同的列值选择列的最后一行 - pandas - Select Last Row of Column Based on Different Column Value 从 Pandas DataFrame 中选择一列中具有相同值但另一列中具有不同值的行 - Select rows from a Pandas DataFrame with same values in one column but different value in the other column Pandas:列上的行操作,在不同列上给定一个参考值 - Pandas: row operations on a column, given one reference value on a different column 选择不同的列python pandas - select different column python pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM