I know there are several ways to convert a column to a date object, but what I am looking for is a way to do so while simultaneously formatting other columns. Say I have the following data frame:
import pandas as pd url = "https://raw.github.com/pandas-dev/pandas/master/pandas/tests/data/tips.csv" df = pd.read_csv(url) df["date"] = list(range(42005, 42005+len(df)))
What I'm trying to achieve is the ability to print these data using some formatting, so I might do something like the following:
print( df .head(10) .to_string( formatters={"total_bill": "${:,.2f}".format, "tip": "${:,.2f}".format } ) )
But I also want to format the date in this step as well. I tried looking through here for what I was looking for, but the datetime options didn't seem like they would work in what I'm trying to do, and building a custom option is a bit outside scope for my target audience.
Is it possible to do this in a simple manner?
1 Answers
Answers 1
Turns out this is incredibly easy once you realize how the function actually works...
print( df .head(10) .to_string( formatters={"total_bill": "${:,.2f}".format, "tip": "${:,.2f}".format, "date": lambda x: "{:%m/%d/%Y}".format(pd.to_datetime(x, unit="D")) } ) ) total_bill tip sex smoker day time size date 0 $16.99 $1.01 Female No Sun Dinner 2 02/08/2017 1 $10.34 $1.66 Male No Sun Dinner 3 02/09/2017 2 $21.01 $3.50 Male No Sun Dinner 3 02/10/2017 3 $23.68 $3.31 Male No Sun Dinner 2 02/11/2017 4 $24.59 $3.61 Female No Sun Dinner 4 02/12/2017 5 $25.29 $4.71 Male No Sun Dinner 4 02/13/2017 6 $8.77 $2.00 Male No Sun Dinner 2 02/14/2017 7 $26.88 $3.12 Male No Sun Dinner 4 02/15/2017 8 $15.04 $1.96 Male No Sun Dinner 2 02/16/2017 9 $14.78 $3.23 Male No Sun Dinner 2 02/17/2017
0 comments:
Post a Comment