熊猫 - 在两列

时间:2018-05-24 03:41:34

标签: pandas

我的数据框有四列。

  • Team1
  • Team2
  • 获胜者
  • Host_Country

现在,我想添加一个新列“Home_Away”,根据“Winner”是否与“Host_Country”相同来填充“Home”或“Away”。最终结果应该是:

  • TEAM1
  • Team2
  • 获胜者
  • Host_Country
  • Home_Away

请有人帮我,我该怎么做。感谢

1 个答案:

答案 0 :(得分:0)

查看问题的解决方案。

import pandas as pd
import numpy as np

df = pd.DataFrame({'Team1': ['Black Eagles',
                             'Banana Slugs',
                             'Preachers',
                             'Fighting Cardinals',
                             'The Predators',
                             'Razorbacks',
                             'Rebels',
                             'Fighting Crusaders',
                             'Avengers',
                             'Aztecs'],
                   'Team2': ['Preachers',
                             'Fighting Cardinals',
                             'The Predators',
                             'Razorbacks',
                             'Rebels',
                             'Fighting Crusaders',
                             'Black Eagles',
                             'Banana Slugs',
                             'Avengers',
                             'Aztecs'],
                   'Winner': ['Preachers',
                              'Fighting Cardinals',
                              'The Predators',
                              'Razorbacks',
                              'Rebels',
                              'Fighting Crusaders',
                              'Black Eagles',
                              'Banana Slugs',
                              'Avengers',
                              'Aztecs'],
                   'Host_Country': ['Black Eagles',
                                    'Banana Slugs',
                                    'Preachers',
                                    'Fighting Cardinals',
                                    'The Predators',
                                    'Razorbacks',
                                    'Rebels',
                                    'Fighting Crusaders',
                                    'Avengers',
                                    'Aztecs']})

df = df[['Team1', 'Team2', 'Winner', 'Host_Country']]

df['Home_Away'] = np.where((df['Winner'] == df['Host_Country']), 'Home', 'Away')

print(df)

,结果将如下所示

               Team1               Team2              Winner                Host_Country        Home_Away  
0        Black Eagles           Preachers           Preachers               Black Eagles        Away  
1        Banana Slugs  Fighting Cardinals  Fighting Cardinals               Banana Slugs        Away  
2           Preachers       The Predators       The Predators                  Preachers        Away  
3  Fighting Cardinals          Razorbacks          Razorbacks         Fighting Cardinals        Away  
4       The Predators              Rebels              Rebels              The Predators        Away  
5          Razorbacks  Fighting Crusaders  Fighting Crusaders                 Razorbacks        Away  
6              Rebels        Black Eagles        Black Eagles                     Rebels        Away  
7  Fighting Crusaders        Banana Slugs        Banana Slugs         Fighting Crusaders        Away  
8            Avengers            Avengers            Avengers                   Avengers        Home  
9              Aztecs              Aztecs              Aztecs                     Aztecs        Home