英文:
Update value dependent on values matching and using index as reference
问题
给定两个数据框:
df_team = pd.DataFrame({
'Team': ["Cal", "Bos", "Flo", "NY", "KC",],
'Con Team': ["California", "Boston", "Florida", "New York", "Kansas City",],
})
df_sched = pd.DataFrame({
'Team': ["Bos", "NY", "KC",],
})
我想执行特定的逻辑。当从df_sched中找到'Team'列的名称时,将df_sched中相同索引位置的值转换为df_team中的'Con Team'列中的值。
results = np.where(df_sched['Team'].isin(df_team['Team']), df_sched['Con Team'], "False")
我只是试图将结果放入一个列表,以尝试理解这个逻辑。当我将真正的语句作为字符串时,至少知道搜索是正确的。现在我只需要将df_sched['Team']中的值转换为df_team['Con Team']中的值。我目前还在学习Python,所以如果有简单的解决方案,请原谅。
英文:
Given 2 dataframes:
df_team = pd.DataFrame({
'Team': ["Cal", "Bos", "Flo", "NY", "KC",],
'Con Team: ["California", "Boston", "Florida", "New York", "Kansas City",],
})
df_sched = pd.DataFrame({
'Team': ["Bos", "NY", "KC",],
})
I would like to perform a certain logic. When the 'Team' name from df_sched is found, convert the value in df_sched to the 'Con Team' in the same index location.
results = np.where(df_sched['Team'].isin(df_team['Team']), df_sched['Con Team'], "False")
I have just been attempting to put into a list in the time being just to try and understand the logic. When I have the true statement as a string, I am getting the results of at least knowing the search is correct. I Just now need it to convert the value in df_sched['Team'] to the value in df_team['Con Team']. Still learning python at the moment so sorry if easy solution.
答案1
得分: 2
使用 pandas.Series.map
函数来匹配两个数据帧之间的 'Team'
列数值:
df_sched['Team'].map(dict(zip(df_team['Team'], df_team['Con Team'])))
0 波士顿
1 纽约
2 堪萨斯城
英文:
Use pandas.Series.map
function to match 'Team'
column values between 2 dataframes:
df_sched['Team'].map(dict(zip(df_team['Team'], df_team['Con Team'])))
0 Boston
1 New York
2 Kansas City
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论