gpt4 book ai didi

python - 为什么这两个表不加入 Python?

转载 作者:太空宇宙 更新时间:2023-11-04 02:35:28 27 4
gpt4 key购买 nike

我正在使用以下代码导入 these sample Kaggle data sets用于 Python 实践):

# importing everything 
import pandas as pd
df_events = pd.DataFrame()
df_ginf = pd.DataFrame()
df_events = pd.read_csv('./events.csv')
df_ginf = pd.read_csv('./ginf.csv')

# creating a match table
eventsList = pd.Series(['On Target', 'Off Target', 'Blocked', 'Hit the Bar'])
eventListKey = pd.Series(['1', '2', '3', '4'])
eventsMatchTable = pd.concat([eventListKey, eventsList], axis = 1)
eventsMatchTable.columns = ['eventKey', 'eventName']
eventsMatchTable['eventKey'] = eventsMatchTable['eventKey'].astype(int)

# trimming the initial dataframe down to something more manageable
df_eventsPlayer = pd.DataFrame()
df_eventsPlayer = df_events[['player', 'event_team', 'opponent', 'shot_place', 'shot_outcome', 'is_goal']]
df_eventsPlayer = df_eventsPlayer.dropna()
df_eventsPlayer['shot_outcome'] =
df_eventsPlayer['shot_outcome'].astype(int)

# attempting the 'merge', here is where the error occurs
df_eventPlayerFinal = pd.DataFrame()
df_eventPlayerFinal = pd.merge(df_eventsPlayer, eventsMatchTable, how = 'left', on = ['shot_outcome','eventKey'])
df_eventPlayerFinal

错误大致说: KeyError: 'shot_outcome' # 验证合并键数据类型。我们可能需要强制

因为我要合并的列都是int,所以这个错误对我来说没有意义。

我错过了什么?

最佳答案

列有不同的名称,因此您不能使用on。应该指定哪个数据集包含给定列:

pd.merge(df_eventsPlayer, eventsMatchTable, how = 'left',
left_on = 'shot_outcome', right_on='eventKey')

参数on两个数据集都包含指定的列时使用

关于python - 为什么这两个表不加入 Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48053470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com