Skip to content

Instantly share code, notes, and snippets.

@aateg
Created June 2, 2019 01:59
Show Gist options
  • Save aateg/b383cb205794db8627ec86f55501bee8 to your computer and use it in GitHub Desktop.
Save aateg/b383cb205794db8627ec86f55501bee8 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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>vel_max</th>\n",
" <th>peso</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>cobra</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>tartaruga</th>\n",
" <td>4</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>pomba</th>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" vel_max peso\n",
"cobra 1 2\n",
"tartaruga 4 5\n",
"pomba 7 8"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# criando dataframe\n",
"df = pd.DataFrame([[1,2],[4,5],[7,8]], index=['cobra','tartaruga','pomba'], columns=['vel_max','peso'])\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Para acessar colunas de uma dataframe basta fazer:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"cobra 1\n",
"tartaruga 4\n",
"pomba 7\n",
"Name: vel_max, dtype: int64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df['vel_max']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mas perceba que isso retorna um elemento do tipo pandas.Series"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Acrescentando colchetes a saída é da forma pandas.DataFrame"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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>vel_max</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>cobra</th>\n",
" <td>1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>tartaruga</th>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>pomba</th>\n",
" <td>7</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" vel_max\n",
"cobra 1\n",
"tartaruga 4\n",
"pomba 7"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[['vel_max']]"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment