使用PythonApply函数向dataframe添加列?

2024-05-16 21:04:10 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有以下数据帧:

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函数。应用功能的输出为以下系列:

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'}),
...

如何将输出字典作为新列添加到我的数据框架中。我想把它们全部八个加到同一行

我很乐意得到任何指导。不一定是代码。也许只要教我怎么做,我会试试

谢谢


Tags: 数据idhomefixteamlasdefenseattack
3条回答

假设您的输出存储在系列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]
df.merge(df.textcol.apply(lambda s: pd.Series({'feature1':s+1, 'feature2':s-1})), 
    left_index=True, right_index=True)

试着这样做:

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)

相关问题 更多 >