Difference between map, applymap and apply methods in Pandas - GeeksforGeeks

13 Dec, 2018
Pandas library is extensively used for data manipulation and analysis. map(), applymap() and apply() methods are methods of Pandas library.
applymap() method only works on a pandas dataframe where function is applied on every element individually.
apply() method can be applied both to series and dataframes where function can be applied both series and individual elements based on the type of function provided.
map() method only works on a pandas series where type of operation to be applied depends on argument passed as a function, dictionary or a list.
Note that the type of Output totally depends on the type of function used as an argument with the given method.
Pandas apply() method:This method which can be used on both on a pandas dataframe and series. The function passed as an argument typically works on rows/columns. Code below illustrates how apply() method works on Pandas dataframe.
# Importing pandas library with an alias pdimport pandas as pd   # Dataframe generationgfg_string = 'geeksforgeeks'gfg_list = 5 * [pd.Series(list(gfg_string))]   gfg_df = pd.DataFrame(data = gfg_list)print("Original dataframe:\n" + \      gfg_df.to_string(index = False,      header = False), end = '\n\n')   # Using apply method for sorting # rows of characters present in # the original dataframenew_gfg_df = gfg_df.apply(lambda x:x.sort_values(), axis = 1)  print("Transformed dataframe:\n" + \       new_gfg_df.to_string(index = False,            header = False), end = '\n\n')
Output:
Below Code illustrates how apply() method on Pandas series:
# Importing pandas library with an alias pdimport pandas as pd   # Series generationgfg_string = 'geeksforgeeks'gfg_series = pd.Series(list(gfg_string))print("Original series\n" + \       gfg_series.to_string(index = False,            header = False), end = '\n\n')   # Using apply method for converting characters# present in the original seriesnew_gfg_series = gfg_series.apply(str.upper)print("Transformed series:\n" + \       new_gfg_series.to_string(index = False,                header = False), end = '\n\n')
Output:
 Pandas applymap() method :This method can be used on a pandas dataframe. The function passed as an argument typically works on elements of the dataframe applymap() is typically used for elementwise operations. Code below illustrates how applymap method works on pandas dataframe:
# Importing pandas library with an alias pdimport pandas as pd   # DataFrame generationgfg_string = 'geeksforgeeks'gfg_list = 5 * [pd.Series(list(gfg_string))]gfg_df = pd.DataFrame(data = gfg_list)  print("Original dataframe:\n" + \       gfg_df.to_string(index = False,        header = False), end = '\n\n')   # Using applymap method for transforming # characters into uppercase characters # present in the original dataframenew_gfg_df = gfg_df.applymap(str.upper)print("Transformed dataframe:\n" + \       new_gfg_df.to_string(index = False,            header = False), end = '\n\n')
Output:
Pandas map() method :This method is used on series function, list and dictionary passed as an argument. This method is generally used to map values from two series having one column same. Code below illustrates how map method works on pandas series:
# Importing pandas library with an alias pdimport pandas as pd   # Series generationgfg_string = 'geeksforgeeks'gfg_series = pd.Series(list(gfg_string))print("Original series\n" + \       gfg_series.to_string(index = False,            header = False), end = '\n\n')   # Using apply method for converting characters# present in the original seriesnew_gfg_series = gfg_series.map(str.upper)print("Transformed series:\n" + \       new_gfg_series.to_string(index = False,                header = False), end = '\n\n')
Output:
pandas-dataframe-program
pandas-series-program
Picked
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Convert integer to string in Python