使用python apply函数将列添加到数据框?

时间:2020-06-11 10:33:22

标签: python pandas

可以说我有以下数据框:

fix_id  lg  home_team    away_team  
9887    30  Leganes      Alaves 
9886    30  Valencia     Las Palmas
9885    30  Celta Vigo   Real Sociedad
9884    30  Girona       Atletico Madrid    

,然后在数据框的所有行上运行Apply函数。 apply函数的输出为以下熊猫系列:

9887   ({'defense': '74', 'midfield': '75', 'attack': '74', 'overall': '75'},
        {'defense': '74', 'midfield': '75', 'attack': '77', 'overall': '75'}),
9886   ({'defense': '80', 'midfield': '80', 'attack': '80', 'overall': '80'},
        {'defense': '75', 'midfield': '74', 'attack': '77', 'overall': '75'}),
...

如何将输出字典作为新列添加到我的数据框。我想将所有八个添加到同一行。

我将很高兴得到任何指导。不一定是代码。也许只是指导我怎么做,我会尝试吗?

谢谢。

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

def mymethod(row):
    # Here whatever operation you have in mind, for example summing two columns of the row:
    return row['A']+row['B']

df['newCol'] = df.apply(lambda row: mymethod(row), axis=1)

答案 1 :(得分:1)

df.merge(df.textcol.apply(lambda s: pd.Series({'feature1':s+1, 'feature2':s-1})), 
    left_index=True, right_index=True)

答案 2 :(得分:1)

假设您的输出存储在s系列中,则可以执行以下操作:

pd.concat([df, s.apply(pd.Series)[0].apply(pd.Series), s.apply(pd.Series)[1].apply(pd.Series)], axis=1)

示例

df = pd.DataFrame({'lg': {9887: 30, 9886: 30, 9885: 30, 9884: 30}, 'home_team': {9887: 'Leganes', 9886: 'Valencia', 9885: 'Celta Vigo', 9884: 'Girona'}, 'away_team': {9887: 'Alaves', 9886: 'Las Palmas', 9885: 'Real Sociedad', 9884: 'Atletico Madrid'}})
s = pd.Series({9887: ({'defense': '74', 'midfield': '75', 'attack': '74', 'overall': '75'}, {'defense': '74', 'midfield': '75', 'attack': '77', 'overall': '75'}), 9886: ({'defense': '80', 'midfield': '80', 'attack': '80', 'overall': '80'}, {'defense': '75', 'midfield': '74', 'attack': '77', 'overall': '75'})})
print(df)
#      lg   home_team        away_team
#9887  30     Leganes           Alaves
#9886  30    Valencia       Las Palmas
#9885  30  Celta Vigo    Real Sociedad
#9884  30      Girona  Atletico Madrid
print(s)
#9887    ({'defense': '74', 'midfield': '75', 'attack':...
#9886    ({'defense': '80', 'midfield': '80', 'attack':...
#dtype: object

df = pd.concat([df, s.apply(pd.Series)[0].apply(pd.Series), s.apply(pd.Series)[1].apply(pd.Series)], axis=1)

#      lg   home_team        away_team defense  ... defense midfield attack overall
#9884  30      Girona  Atletico Madrid     NaN  ...     NaN      NaN    NaN     NaN
#9885  30  Celta Vigo    Real Sociedad     NaN  ...     NaN      NaN    NaN     NaN
#9886  30    Valencia       Las Palmas      80  ...      75       74     77      75
#9887  30     Leganes           Alaves      74  ...      74       75     77      75

[4 rows x 11 columns]