Format data
Change the type of data in a column, like converting text to numbers or dates.
Example Data
Follow along with right out of the box example data. Copy following data in the information request of the agent you are working in.
When data is first imported, every column is treated as text — even numbers, currencies, or dates. But for analysis to work correctly, columns must have the right type. For example, to compare transaction amounts or sort dates chronologically, you need to convert those columns into numbers or datetime format. This is similar to switching a column from “Text” to “Number” or “Date” in Excel.
Convert a column to numeric
Excel
In Excel, you change the format of a cell or column to "Number" using the toolbar.
t0 Prompt
Convert the "Amount" column to a number
Change transaction amount to numeric
Code
The python code looks as follows:
Convert a column to dates
Excel
In Excel, you can change the format of a cell to "Date" so it sorts correctly or works in formulas.
t0 Prompt
Format the date column to date
Convert the text dates to real dates
Code
The python code looks as follows:
Convert a column to text
Excel
In Excel, you might apply the “Text” format to force values to stay as-is, like codes or categories.
t0 Prompt
Convert seller column to text
Change format of buyer to text
Code
The python code looks as follows:
Function | Description |
---|---|
pd.to_numeric(column) | Converts a column to numbers |
pd.to_datetime(column) | Converts a column to datetime |
column.astype(str) | Converts a column to string (text) |