1

Lets say I have the following dataframe:

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    

and I run an apply function over all the rows of the dataframe. The output of the apply function is the following pandas 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'}),
...

How could add the output dictionaries as new columns to my dataframe. I want to add all eight of them to the same row.

I will be glad to get any guidance. Not necessarily a code. Maybe just instruct me how to, and I will try?

Thanks.

3 Answers 3

1

Supposing your output is stored in Series s you can do the following:

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

Example

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Amazing. Thank you!
1

Try something like this:

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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.