Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply Formatting to Each Column in Dataframe Using a Dict Mapping

Problem Setup

import pandas as pd
df = pd.DataFrame(data={'Currency': {0: 111.23, 1: 321.23},
                   'Int': {0: 23, 1: 3},
                   'Rate': {0: 0.03030, 1: 0.09840}}
            )

Produces the following DataFrame

   Currency  Int    Rate
0    111.23   23  0.0303
1    321.23    3  0.0984

I want to apply very specific formatting to each column in the dataframe using a dict like the following:

format_mapping={'Currency': '${:,.2f}', 'Int': '{:,.0f}', 'Rate': '{:.2f}%'}

I know I can use applymap for multiple columns or apply on a single column:

#All columns
df = df.applymap('{:.2f}%'.format)
#Specific columns
df['Rate'] = df['Rate'].apply('{:.2f}%'.format)

Question

How can I iterate through each column in a dataframe and apply formatting using a dictionary where the dict key is the column and the value is the string formatting?

End result would look like this (ignore the fact that percent wasn't multiplied by 100 for now)

  Currency Int   Rate
0  $111.23  23  0.03%
1  $321.23   3  0.10%
like image 512
Jarad Avatar asked Sep 23 '15 16:09

Jarad


People also ask

What is the difference between map () Applymap and apply?

What is the difference between map(), applymap() and apply() methods in pandas? – In padas, all these methods are used to perform either to modify the DataFrame or Series. map() is a method of Series, applymap() is a method of DataFrame, and apply() is defined in both DataFrame and Series.

What pandas method will you use to map columns between two data frames?

Pandas: Series - map() function The map() function is used to map values of Series according to input correspondence. Used for substituting each value in a Series with another value, that may be derived from a function, a dict or a Series.

How do you apply a map to a DataFrame in Python?

applymap() method applies a function that accepts and returns a scalar to every element of a DataFrame. Syntax: DataFrame. applymap(func) Parameters: func: Python function, returns a single value from a single value. Returns: Transformed DataFrame.


2 Answers

The easiest way would be to iterate through the format_mapping dictionary and then apply on the column (denoted by the key) the formatting denoted by the value. Example -

for key, value in format_mapping.items():
    df[key] = df[key].apply(value.format)

Demo -

In [62]: df = pd.DataFrame(data={'Currency': {0: 111.23, 1: 321.23},
   ....:                    'Int': {0: 23, 1: 3},
   ....:                    'Rate': {0: 0.03030, 1: 0.09840}}
   ....:             )

In [63]:

In [63]: format_mapping={'Currency': '${:,.2f}', 'Int': '{:,.0f}', 'Rate': '{:.2f}%'}

In [64]: for key, value in format_mapping.items():
   ....:     df[key] = df[key].apply(value.format)
   ....:

In [65]: df
Out[65]:
  Currency Int   Rate
0  $111.23  23  0.03%
1  $321.23   3  0.10%
like image 199
Anand S Kumar Avatar answered Oct 20 '22 13:10

Anand S Kumar


In 2021 (Pandas 1.2.3) you can use df.style.format():

import pandas as pd

df = pd.DataFrame(
    data={
        "Currency": {0: 111.23, 1: 321.23},
        "Int": {0: 23, 1: 3},
        "Rate": {0: 0.03030, 1: 0.09840},
    }
)
format_mapping = {"Currency": "${:,.2f}", "Int": "{:,.0f}", "Rate": "{:.2f}%"}

df.style.format(format_mapping)

styled pandas dataframe

More information: https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Finer-Control:-Display-Values

like image 31
Stefan_EOX Avatar answered Oct 20 '22 13:10

Stefan_EOX