pandas
In [5]:
Copied!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Listas, dicionários e vetores¶
In [6]:
Copied!
lista_01 = [1,5,13,4,6,8] # lista
dict_01 = {"valor x": 1, "valor y": 32, "valor z": 3.5} # dicionário
array_01 = np.array([1,2.3,2.4])
lista_01 = [1,5,13,4,6,8] # lista
dict_01 = {"valor x": 1, "valor y": 32, "valor z": 3.5} # dicionário
array_01 = np.array([1,2.3,2.4])
In [3]:
Copied!
lista_01[1]
lista_01[1]
Out[3]:
5
In [4]:
Copied!
dict_01['valor y']
dict_01['valor y']
Out[4]:
32
In [7]:
Copied!
array_01[2]
array_01[2]
Out[7]:
2.4
In [12]:
Copied!
lista_01.append('3.0')
lista_01
lista_01.append('3.0')
lista_01
Out[12]:
[1, 5, 13, 4, 6, 8, '3']
In [11]:
Copied!
array_01 = np.append(array_01, 3.)
array_01
array_01 = np.append(array_01, 3.)
array_01
Out[11]:
array([1. , 2.3, 2.4, 3. ])
In [ ]:
Copied!
dict_01['valor i'] = 4
dict_01.update({'valor j': 4, 'valor k': 5})
dict_01['valor i'] = 4
dict_01.update({'valor j': 4, 'valor k': 5})
Dataframes¶
In [17]:
Copied!
idade= np.random.randint(low=12, high=20, size=14)
idade
idade= np.random.randint(low=12, high=20, size=14)
idade
Out[17]:
array([13, 19, 12, 16, 18, 17, 19, 19, 17, 16, 12, 16, 17, 17])
In [18]:
Copied!
dados = {'Nome': ['Joelande','Jociara', 'Basilio', 'Mahysa', 'Carlos', 'João Paulo', 'Nilton', 'Ana Paula', 'Hermilino Danilo', 'Paulo Tadeu', 'Evaldo', 'Fernando' , 'Lorena', 'Jailza'],
'Idade': idade
}
dados = {'Nome': ['Joelande','Jociara', 'Basilio', 'Mahysa', 'Carlos', 'João Paulo', 'Nilton', 'Ana Paula', 'Hermilino Danilo', 'Paulo Tadeu', 'Evaldo', 'Fernando' , 'Lorena', 'Jailza'],
'Idade': idade
}
In [14]:
Copied!
df = pd.DataFrame(dados)
df.head()
df = pd.DataFrame(dados)
df.head()
Out[14]:
Nome | idade | |
---|---|---|
0 | Joelande | 14 |
1 | Jociara | 18 |
2 | Basilio | 15 |
3 | Mahysa | 13 |
4 | Carlos | 19 |
Carregando um dataframe de um arquivo¶
In [33]:
Copied!
df = pd.read_csv('https://raw.githubusercontent.com/255ribeiro/curso_python_gis/master/docs/dados_py/heroes_information.csv')
print(df.shape)
df.head()
df = pd.read_csv('https://raw.githubusercontent.com/255ribeiro/curso_python_gis/master/docs/dados_py/heroes_information.csv')
print(df.shape)
df.head()
(734, 11)
Out[33]:
Unnamed: 0 | name | Gender | Eye color | Race | Hair color | Height | Publisher | Skin color | Alignment | Weight | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | A-Bomb | Male | yellow | Human | No Hair | 203.0 | Marvel Comics | - | good | 441.0 |
1 | 1 | Abe Sapien | Male | blue | Icthyo Sapien | No Hair | 191.0 | Dark Horse Comics | blue | good | 65.0 |
2 | 2 | Abin Sur | Male | blue | Ungaran | No Hair | 185.0 | DC Comics | red | good | 90.0 |
3 | 3 | Abomination | Male | green | Human / Radiation | No Hair | 203.0 | Marvel Comics | - | bad | 441.0 |
4 | 4 | Abraxas | Male | blue | Cosmic Entity | Black | -99.0 | Marvel Comics | - | bad | -99.0 |
In [ ]:
Copied!
In [42]:
Copied!
fltr = df['Gender'] == 'Female'
df_female = df[fltr]
df_female.head()
fltr = df['Gender'] == 'Female'
df_female = df[fltr]
df_female.head()
Out[42]:
Unnamed: 0 | name | Gender | Eye color | Race | Hair color | Height | Publisher | Skin color | Alignment | Weight | |
---|---|---|---|---|---|---|---|---|---|---|---|
8 | 8 | Agent 13 | Female | blue | - | Blond | 173.0 | Marvel Comics | - | good | 61.0 |
24 | 24 | Angel Dust | Female | yellow | Mutant | Black | 165.0 | Marvel Comics | - | good | 57.0 |
25 | 25 | Angel Salvadore | Female | brown | - | Black | 163.0 | Marvel Comics | - | good | 54.0 |
26 | 26 | Angela | Female | - | - | - | -99.0 | Image Comics | - | bad | -99.0 |
38 | 38 | Arachne | Female | blue | Human | Blond | 175.0 | Marvel Comics | - | good | 63.0 |
In [41]:
Copied!
fltr = (df['Gender'] == 'Female') & (df['Race'] == 'Mutant')
df_female_mut = df[fltr]
df_female_mut.head()
fltr = (df['Gender'] == 'Female') & (df['Race'] == 'Mutant')
df_female_mut = df[fltr]
df_female_mut.head()
Out[41]:
Unnamed: 0 | name | Gender | Eye color | Race | Hair color | Height | Publisher | Skin color | Alignment | Weight | |
---|---|---|---|---|---|---|---|---|---|---|---|
24 | 24 | Angel Dust | Female | yellow | Mutant | Black | 165.0 | Marvel Comics | - | good | 57.0 |
55 | 55 | Aurora | Female | blue | Mutant | Black | 180.0 | Marvel Comics | - | good | 63.0 |
114 | 114 | Blink | Female | green | Mutant | Magenta | 165.0 | Marvel Comics | pink | good | 56.0 |
129 | 129 | Boom-Boom | Female | blue | Mutant | Blond | 165.0 | Marvel Comics | - | good | 55.0 |
167 | 167 | Cerebra | Female | - | Mutant | - | -99.0 | Marvel Comics | - | good | -99.0 |
In [43]:
Copied!
df_female_mut['Weight'].mean()
df_female_mut['Weight'].mean()
Out[43]:
41.0
In [48]:
Copied!
df_gb = df_female_mut.groupby('Alignment')
df_gb['Weight'].mean()
df_gb = df_female_mut.groupby('Alignment')
df_gb['Weight'].mean()
Out[48]:
Alignment bad 56.500000 good 37.647059 neutral 67.000000 Name: Weight, dtype: float64