DanZimmerman
DanZimmerman

Reputation: 1716

Python - format dataframe columns as different datatypes

I have a dataframe:

df = pd.DataFrame({'a':[1,2,3], 'b':['2017/01/01', '2017/01/02','2016/12/31'], 'c':['aaa', 'bbb', 'ccc'], 'd':[4,5,6]})

enter image description here

I have a list of formatters:

formatter = [4.2, '%Y%m%d', None, 8.2]

I want to format column a as float '4.2f', column b as strftime('%Y%m'), column c as it is (string, no need to format it), and column d as float '8.2f'. How do I pass this list of formatters to dataframe df?

Thanks,

Upvotes: 2

Views: 2628

Answers (1)

Raquel Guimarães
Raquel Guimarães

Reputation: 982

If you switch from a list of formatters to a map based on columns you can use the style.format on a dataframe.

Something like

import pandas as pd
import datetime

def time_formatter(data):
    return datetime.datetime.strptime(data, "%Y/%m/%d").date().strftime('%Y%m%d')

df = pd.DataFrame({'a':[1,2,3], 'b':['2017/01/01', '2017/01/02','2016/12/31'], 'c':['aaa', 'bbb', 'ccc'], 'd':[4,5,6]})

formatter = {'a':'{:4.2f}', 'b': time_formatter, 'd':'{:8.2f}'}

df.style.format(formatter)

will output

    a       b           c   d
0   1.00    20170101    aaa 4.00
1   2.00    20170102    bbb 5.00
2   3.00    20161231    ccc 6.00

Edit:

There must be a cleaner way, but to actually update the dataframe with the format you could do something like:

df['a'] = df['a'].map('{:4.2f}'.format)
df['d'] = df['d'].map('{:8.2f}'.format)
df['b'] = df['b'].map(time_formatter)

Or for a more generic (and cryptic) way:

formatter = {'a':'{:4.2f}'.format, 'b': time_formatter, 'd':'{:8.2f}'.format}

for f in formatter.items():
    column = f[0]
    df[column] = df[column].map(f[1])

Upvotes: 4

Related Questions