简体   繁体   English

python:pandas:按条件将其他 dataframe 的值添加到新列中

[英]python: pandas: add values from other dataframe into new column by condition

I have two dataframes with the following data:我有两个包含以下数据的数据框:

fixtures = pd.DataFrame(
    {'HomeTeam': ["A", "B", "C", "D"], 'AwayTeam': ["E", "F", "G", "H"]})

ratings = pd.DataFrame({'team': ["A", "B", "C", "D", "E", "F", "G", "H"], "rating": [
                       "1,5", "0,2", "0,5", "2", "3", "4,8", "0,9", "-0,4"]})

now i want to map the value from ratings["rating"] to the respective team names but i can't get it to work.现在我想要 map 从ratings["rating"]到各自团队名称的值,但我无法让它工作。 is it possible to have new columns with the ratings appear to the right of the HomeTeam and AwayTeam columns?是否可以在HomeTeamAwayTeam列的右侧显示评级的新列?

expected output:预计 output:

fixtures:固定装置:

homeTeam  homeTeamRating  awayTeam  AwayTeamRating  
Team A    1,5             Team E    3

you can use:您可以使用:

to_replace=dict(zip(ratings.team,ratings.rating)) #create a dict. Key is team name value is rating.
#{'A': '1,5', 'B': '0,2', 'C': '0,5', 'D': '2', 'E': '3', 'F': '4,8', 'G': '0,9', 'H': '-0,4'}

fixtures['homeTeamRating']=fixtures['HomeTeam'].map(to_replace) #use map and  replace team column as a new column.
fixtures['AwayTeamRating']=fixtures['AwayTeam'].map(to_replace)

fixtures=fixtures[['HomeTeam','homeTeamRating','AwayTeam','AwayTeamRating']]

'''
  HomeTeam homeTeamRating AwayTeam AwayTeamRating
0        A            1,5        E              3
1        B            0,2        F            4,8
2        C            0,5        G            0,9
3        D              2        H           -0,4
'''

If you need to apply a method over an existing column in order to compute some values that will eventually be added as a new column in the existing DataFrame , then pandas.DataFrame.apply() method should do the trick.如果您需要在现有列上应用一种方法以计算最终将作为新列添加到现有DataFrame中的一些值,那么pandas.DataFrame.apply()方法应该可以解决问题。

暂无
暂无

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

相关问题 Pandas/Python:如何根据其他列的值创建新列并将额外条件应用于此新列 - Pandas/Python: How to create new column based on values from other columns and apply extra condition to this new column 如果ID存在于其他数据帧中,则Python Pandas数据帧会在新列中添加“1” - Python Pandas dataframe add “1” in new column if ID exists in other dataframe 根据另一列中的值将列添加到Python pandas DataFrame - Add column to a Python pandas DataFrame based on values in an other column 根据其他列值计算出的新的Pandas Dataframe列 - new Pandas Dataframe column calculated from other column values Pandas:添加新列并按条件从另一个dataframe赋值 - Pandas: Add new column and assigning value from another dataframe by condition 向 pandas dataframe 添加一个新列,其中包含来自另一列的转换值? - Add a new column to pandas dataframe with coverted values from another column? Python Pandas新的dataframe列有group by和condition - Python Pandas new dataframe column with group by and condition 使用函数pandas python基于来自其他列的值添加新列 - add a new column based on values from other column using a function pandas python python pandas dataframe从其他列的单元格创建新列 - python pandas dataframe create new column from other columns' cells 根据 Pandas DataFrame 中其他列的条件创建新列 - Creating New Column based on condition on Other Column in Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM