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?
In Pandas, you can convert a column (string/object or integer type) to datetime using the to_datetime() and astype() methods.
In order to be able to work with it, we are required to convert the dates into the datetime format. Code #1 : Convert Pandas dataframe column type from string to datetime format using pd.to_datetime() function. Output : As we can see in the output, the data type of the ‘Date’ column is object i.e. string.
You may refer to the following source for the different formats that you may apply. For our example, the complete Python code to convert the strings to datetime would be: import pandas as pd values = {'dates': ['20190902','20190913','20190921'], 'status': ['Opened','Opened','Closed'] } df = pd.DataFrame (values, ...
Example 3: Convert an Entire DataFrame to Strings. Lastly, we can convert every column in a DataFrame to strings by using the following syntax: #convert every column to strings df = df.astype (str) #check data type of each column df.dtypes player object points object assists object dtype: object.
As we can see in the output, the data type of the ‘Date’ column is object i.e. string. Now we will convert it to datetime format using DataFrame.astype () function. As we can see in the output, the format of the ‘Date’ column has been changed to the datetime format.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With