从多个列中生成pandas dataframe行值的列表

2024-05-29 04:41:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在apandas.DataFrame中有这些数据:

Date, Team1, Team2, Team1 Score, Team2 Score, Event
8/2/17, Juventus, Milan, 2, 1, Friendly match
6/2/17, Milan, Napoli, 3, 0, Friendly match
5/1/17, Milan, Sampdoria, 1, 0, Friendly match
25/12/16, Parma, Milan, 0, 5, Friendly match

我怎样才能列出米兰的进球?

输出应该如下所示:

[1, 3, 1, 5]

Tags: 数据eventdataframedatematchparmascorefriendly
3条回答
# slice df with just team columns and get values
t = df[['Team1', 'Team2']].values

# find the row and column slices where equal to 'Milan'
i, j = np.where(t == 'Milan')

# then slice the scores array with those positions
s = df[['Team1 Score', 'Team2 Score']].values

s[i, j]

array([1, 3, 1, 5])

我可以进一步压缩,因为我知道所有列在哪里

v = df.values
i, j = np.where(v[:, [1, 2]] == 'Milan')
v[:, [3, 4]][i, j]

array([1, 3, 1, 5])

这将完成以下工作:

pd.concat([df["Team1 Score"][df.Team1=='Milan'],df["Team2 Score"][df.Team2=='Milan']]).sort_index().values.tolist()

输出是[1, 3, 1, 5]

可以使用numpy数组的布尔索引,这里使用values来获取2D numpy数组,并使用布尔索引来获取TeamMilan的值:

df[["Team1 Score", "Team2 Score"]].values[df[["Team1", "Team2"]] == "Milan"]
# array([1, 3, 1, 5])

相关问题 更多 >

    热门问题