Skip to content

Instantly share code, notes, and snippets.

@LowriWilliams
Created May 20, 2020 09:54
Show Gist options
  • Save LowriWilliams/dc824a915a1260efc9107f8dd9b2e182 to your computer and use it in GitHub Desktop.
Save LowriWilliams/dc824a915a1260efc9107f8dd9b2e182 to your computer and use it in GitHub Desktop.
corona/data_type_change
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 770 entries, 0 to 769\n",
"Data columns (total 8 columns):\n",
"Sno 770 non-null int64\n",
"Date 770 non-null object\n",
"Province/State 585 non-null object\n",
"Country 770 non-null object\n",
"Last Update 770 non-null object\n",
"Confirmed 770 non-null float64\n",
"Deaths 770 non-null float64\n",
"Recovered 770 non-null float64\n",
"dtypes: float64(3), int64(1), object(4)\n",
"memory usage: 48.2+ KB\n",
"\n",
"\n",
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 770 entries, 0 to 769\n",
"Data columns (total 8 columns):\n",
"Sno 770 non-null int64\n",
"Date 770 non-null datetime64[ns]\n",
"Province/State 585 non-null object\n",
"Country 770 non-null object\n",
"Last Update 770 non-null object\n",
"Confirmed 770 non-null float64\n",
"Deaths 770 non-null float64\n",
"Recovered 770 non-null float64\n",
"dtypes: datetime64[ns](1), float64(3), int64(1), object(3)\n",
"memory usage: 48.2+ KB\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Sno</th>\n",
" <th>Date</th>\n",
" <th>Province/State</th>\n",
" <th>Country</th>\n",
" <th>Last Update</th>\n",
" <th>Confirmed</th>\n",
" <th>Deaths</th>\n",
" <th>Recovered</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>2020-01-22</td>\n",
" <td>Anhui</td>\n",
" <td>China</td>\n",
" <td>01/22/2020 12:00:00</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>2</td>\n",
" <td>2020-01-22</td>\n",
" <td>Beijing</td>\n",
" <td>China</td>\n",
" <td>01/22/2020 12:00:00</td>\n",
" <td>14.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>3</td>\n",
" <td>2020-01-22</td>\n",
" <td>Chongqing</td>\n",
" <td>China</td>\n",
" <td>01/22/2020 12:00:00</td>\n",
" <td>6.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4</td>\n",
" <td>2020-01-22</td>\n",
" <td>Fujian</td>\n",
" <td>China</td>\n",
" <td>01/22/2020 12:00:00</td>\n",
" <td>1.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5</td>\n",
" <td>2020-01-22</td>\n",
" <td>Gansu</td>\n",
" <td>China</td>\n",
" <td>01/22/2020 12:00:00</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" <td>0.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Sno Date Province/State Country Last Update Confirmed \\\n",
"0 1 2020-01-22 Anhui China 01/22/2020 12:00:00 1.0 \n",
"1 2 2020-01-22 Beijing China 01/22/2020 12:00:00 14.0 \n",
"2 3 2020-01-22 Chongqing China 01/22/2020 12:00:00 6.0 \n",
"3 4 2020-01-22 Fujian China 01/22/2020 12:00:00 1.0 \n",
"4 5 2020-01-22 Gansu China 01/22/2020 12:00:00 0.0 \n",
"\n",
" Deaths Recovered \n",
"0 0.0 0.0 \n",
"1 0.0 0.0 \n",
"2 0.0 0.0 \n",
"3 0.0 0.0 \n",
"4 0.0 0.0 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Let's look at the column data types\n",
"\n",
"df.info()\n",
"\n",
"# Convert Date column to datetime64 format\n",
"\n",
"df['Date'] = df['Date'].apply(pd.to_datetime)\n",
"\n",
"# Check whether the Date data type was successfully changed to datetime64\n",
"\n",
"print(\"\\n\")\n",
"df.info()\n",
"\n",
"# Access date element from Date column\n",
"\n",
"df['Date'] = df['Date'].dt.date\n",
"\n",
"display(df.head())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment