Q & A – Pandas Advance Interview Questions


(1) Difference Between “apply()” and “applymap()” Method In Pandas.

  • Main difference between “apply()” and  “applymap()” is that,
  • “apply()” method applies the function entirely by taking a row or column as an argument.
  • “applymap()” method applies the function elementwise to each row or each column.

Example

import pandas as pd
df1 = pd.DataFrame({'names': ['Subrat', 'Arpita', 'Abhispa', 'Subhada', 'Sonali'], 
                    'marks': [67, 75, 84, 90, 99]})

df1
df1.apply(lambda x:len(x))

Output:

Note:

  • Here ‘5’ represents the total elements inside the ‘name’ and ‘marks’ columns.
  • Here apply() considered ‘name’ and ‘column’ as a series and calculated its length.
df1.applymap(lambda x: len(str(x)))

Output:

Note:

  • Here applymap() method calculated the length of individual elements inside the dataframe.

Leave a Reply

Your email address will not be published. Required fields are marked *