Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert to date using formatters parameter in pandas to_string

Tags:

python

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?

like image 281
tblznbits Avatar asked Feb 20 '17 15:02

tblznbits


People also ask

What is the method in pandas to change values into date time data type?

In Pandas, you can convert a column (string/object or integer type) to datetime using the to_datetime() and astype() methods.

How to convert string to datetime format in pandas Dataframe?

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.

How to convert string to datetime in Python?

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, ...

How do I convert a Dataframe to a string in Python?

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.

How to change the format of ‘Date’ column in a Dataframe?

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.


1 Answers

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
like image 164
tblznbits Avatar answered Oct 19 '22 18:10

tblznbits