Python uses PyJanitor's to_ Datetime, to_ Numeric, to_ String and other functions for data conversion
Preparation work:
1. Install Python 3. x version and corresponding package manager, such as pip.
2. Create a new Python project and set up the Python interpreter.
Dependent class libraries:
1. Pandas: Used for data processing and conversion.
2. PyJanitor: Used to extend the data conversion function of Pandas.
Install dependent libraries:
You can use the following command to install the required dependent libraries:
python
pip install pandas pyjanitor
Data sample:
To demonstrate PyJanitor's data conversion function, we use the following sample data:
import pandas as pd
data = {
'date': ['2020-01-01', '2020-02-01', '2020-03-01'],
'numeric': ['10', '20', '30'],
'string': ['apple', 'banana', 'orange']
}
df = pd.DataFrame(data)
The Dataframe is as follows:
date numeric string
0 2020-01-01 10 apple
1 2020-02-01 20 banana
2 2020-03-01 30 orange
Using PyJanitor for data conversion:
python
import pandas as pd
import janitor
data = {
'date': ['2020-01-01', '2020-02-01', '2020-03-01'],
'numeric': ['10', '20', '30'],
'string': ['apple', 'banana', 'orange']
}
df = pd.DataFrame(data)
#Convert date columns to date time type
df = df.to_datetime('date', errors='coerce', format='%Y-%m-%d')
#Convert numeric columns to numeric type
df = df.to_numeric('numeric', errors='coerce')
#Convert string columns to string type
df = df.to_string('string')
#Print the converted results
print(df.dtypes)
print(df)
Output results:
date datetime64[ns]
numeric int64
string object
dtype: object
date numeric string
0 2020-01-01 10 apple
1 2020-02-01 20 banana
2 2020-03-01 30 orange
Summary:
PyJanitor is a Python library for data conversion that can extend the functionality of Pandas. It provides a series of convenient functions, such as' to '_ Datetime, to_ Numeric, to_ String `, etc., used to convert data to the specified type. Before using PyJanitor, it is necessary to ensure that Pandas and PyJanitor are installed, and then call relevant functions as needed.