0% found this document useful (0 votes)
11 views9 pages

Snippets Ipynb

The document shows how to create a DataFrame from a dictionary of data, sort and filter the DataFrame. Key steps include: 1. A dictionary of people data is created with keys of 'first', 'last', and 'email'. 2. Pandas is imported and the dictionary is converted to a DataFrame. 3. The DataFrame is sorted by the 'last' column in descending order. 4. The DataFrame is sorted by the 'last' and 'first' columns with different sort directions.

Uploaded by

Agoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Snippets Ipynb

The document shows how to create a DataFrame from a dictionary of data, sort and filter the DataFrame. Key steps include: 1. A dictionary of people data is created with keys of 'first', 'last', and 'email'. 2. Pandas is imported and the dictionary is converted to a DataFrame. 3. The DataFrame is sorted by the 'last' column in descending order. 4. The DataFrame is sorted by the 'last' and 'first' columns with different sort directions.

Uploaded by

Agoy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

{

"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"people = {\n",
" 'first': ['Corey', 'Jane', 'John', 'Adam'], \n",
" 'last': ['Schafer', 'Doe', 'Doe', 'Doe'], \n",
" 'email': ['[email protected]', '[email protected]',
'[email protected]', '[email protected]']\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(people)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"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>first</th>\n",
" <th>last</th>\n",
" <th>email</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Corey</td>\n",
" <td>Schafer</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Jane</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>John</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Adam</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" first last email\n",
"0 Corey Schafer [email protected]\n",
"1 Jane Doe [email protected]\n",
"2 John Doe [email protected]\n",
"3 Adam Doe [email protected]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"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>first</th>\n",
" <th>last</th>\n",
" <th>email</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Corey</td>\n",
" <td>Schafer</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Jane</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>John</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Adam</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" first last email\n",
"0 Corey Schafer [email protected]\n",
"1 Jane Doe [email protected]\n",
"2 John Doe [email protected]\n",
"3 Adam Doe [email protected]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.sort_values(by='last', ascending=False)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"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>first</th>\n",
" <th>last</th>\n",
" <th>email</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Corey</td>\n",
" <td>Schafer</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>John</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Jane</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Adam</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" first last email\n",
"0 Corey Schafer [email protected]\n",
"2 John Doe [email protected]\n",
"1 Jane Doe [email protected]\n",
"3 Adam Doe [email protected]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.sort_values(by=['last', 'first'], ascending=False)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"df.sort_values(by=['last', 'first'], ascending=[False, True], inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"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>first</th>\n",
" <th>last</th>\n",
" <th>email</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Corey</td>\n",
" <td>Schafer</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Adam</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Jane</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>John</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" first last email\n",
"0 Corey Schafer [email protected]\n",
"3 Adam Doe [email protected]\n",
"1 Jane Doe [email protected]\n",
"2 John Doe [email protected]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"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>first</th>\n",
" <th>last</th>\n",
" <th>email</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Corey</td>\n",
" <td>Schafer</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Jane</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>John</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Adam</td>\n",
" <td>Doe</td>\n",
" <td>[email protected]</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" first last email\n",
"0 Corey Schafer [email protected]\n",
"1 Jane Doe [email protected]\n",
"2 John Doe [email protected]\n",
"3 Adam Doe [email protected]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.sort_index()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3 Doe\n",
"1 Doe\n",
"2 Doe\n",
"0 Schafer\n",
"Name: last, dtype: object"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['last'].sort_values()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

You might also like