Question
Is there a way to format only a specific dataframe?
I've seen examples of formatting specific columns of a single dataframe (Example 1) or set the entire pandas library to a default option (Example 2). However, I haven't seen an option for formatting a specific dataframe without explicitly listing each column.
Setup
import pandas as pd
import numpy as np
# Setup first example
data = np.random.random((3,4))
df = pd.DataFrame(data)
print df
# 0 1 2 3
#0 0.384326 0.364187 0.084034 0.012376
#1 0.114784 0.298068 0.087634 0.828207
#2 0.255923 0.438617 0.820652 0.266964
Example 1 - Change format for specific column(s) in a single dataframe
df[3] = df[3].map('${:,.2f}'.format)
print df
# 0 1 2 3
#0 0.384326 0.364187 0.084034 $0.01
#1 0.114784 0.298068 0.087634 $0.83
#2 0.255923 0.438617 0.820652 $0.27
Example 2 - Change format for all pandas dataframes (including new ones)
pd.options.display.float_format = '${:,.2f}'.format
print(df)
# 0 1 2 3
#0 $0.38 $0.36 $0.08 $0.01
#1 $0.11 $0.30 $0.09 $0.83
#2 $0.26 $0.44 $0.82 $0.27
data2 = np.random.random((4,3))
df2 = pd.DataFrame(data2)
print df2
# 0 1 2
#0 $0.60 $0.37 $0.86
#1 $0.28 $0.06 $0.97
#2 $0.19 $0.68 $0.99
#3 $0.06 $0.88 $0.82
I was looking for an option like example 2, except it won't apply the formatting to future dataframes. Thanks!
EDIT - My apologies, I should've been clearer about the formatting. Example 1 changes the data type while Example 2 doesn't. I was hoping to not have to convert between data types (if possible). E.g. The first example changes from floats to non-null objects:
df.info()
#<class 'pandas.core.frame.DataFrame'>
#Int64Index: 3 entries, 0 to 2
#Data columns (total 4 columns):
#0 3 non-null float64
#1 3 non-null float64
#2 3 non-null float64
#3 3 non-null object
#dtypes: float64(3), object(1)