Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Created November 29, 2017 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinThoma/e320cbb937afb4ff766f75988f1c65e6 to your computer and use it in GitHub Desktop.
Save MartinThoma/e320cbb937afb4ff766f75988f1c65e6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""What is the difference between map and apply?"""
import pandas as pd
df = pd.DataFrame([(1, 2, 3), (5, 6, 7), (9, 0, 1), (3, 4, 5)],
columns=list('abc'),
index=['India', 'France', 'England', 'Germany'])
df['b'] = df['a']
df['c'] = df['a']
print(df)
# Is there a difference between .map and .apply?
df['b'] = df['b'].map(lambda x: x**2)
df['c'] = df['c'].apply(lambda x: x**2)
print(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment