Search code examples
pandasdataframepython-applymap

Understand pandas' applymap argument


I'm trying to highlight specific columns in my dataframe using guideline from this post, https://stackoverflow.com/a/41655055/5158984.

My question is on the use of the subset argument. My guess is that it's part of the **kwargs argument. However, the official documentation from Pandas, https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.applymap.html, vaguely explains it.

So in general, how can I know which key words I can use whenever I see **kwargs?

Thanks!


Solution

  • It seems that you are confusing pandas.DataFrame.applymap and df.style.applymap (where df is an instance of pd.DataFrame), for which subset stands on its own and is not part of the kwargs arguments.

    Here is one way to find out (in your terminal or a Jupyter notebook cell) what are the named parameters of this method (or any other Pandas method for that matter):

    import pandas as pd
    
    df = pd.DataFrame()
    help(df.style.applymap)
    
    # Output
    
    Help on method applymap in module pandas.io.formats.style:
    
    applymap(func: 'Callable', subset: 'Subset | None' = None, **kwargs) 
    -> 'Styler' method of pandas.io.formats.style.Styler instance
        Apply a CSS-styling function elementwise.
        
        Updates the HTML representation with the result.
        
        Parameters
        ----------
        func : function
            ``func`` should take a scalar and return a string.
        
        subset : label, array-like, IndexSlice, optional
            A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input
            or single key, to `DataFrame.loc[:, <subset>]` where the columns are
            prioritised, to limit ``data`` to *before* applying the function.
        
        **kwargs : dict
            Pass along to ``func``. 
       ...