In [1]:
import numpy as np
import pandas as pd
from pandas import DataFrame, Series
In [2]:
serieA = Series(np.arange(6),index=['A','B','C','D','E','F'])
In [3]:
serieA
Out[3]:
In [4]:
serieA['E'] #selecionar el elemento E
Out[4]:
In [5]:
serieA[1:4] #seleccionar por rango o slice
Out[5]:
In [6]:
serieA[['B','F','A','R']] #Seleccionar por indice, en el caso de R, el retorno sera NaN
Out[6]:
In [7]:
serieA[serieA>4] # Seleccionar por condición
Out[7]:
In [8]:
serieA[serieA>3]= 0 #Seleccionar por condición y asignar un valor
In [9]:
ind = list('ABCDEFGHIJ') # Definir indices
c = list('WXYZ') # Definir columnas
df = DataFrame(np.random.randint(100, size=40).reshape(10,4),index=ind,columns=c)
df
Out[9]:
In [10]:
df
Out[10]:
In [11]:
df[1:5] # Seleccionar los registros por slice de 1 a 5
Out[11]:
In [12]:
df[df['W']<=10] # Seleccionar por condición
Out[12]:
In [13]:
df > 50 # Mostrar entradas mayores a 50
Out[13]:
In [14]:
df.ix['E'] #Seleccionar un index
Out[14]:
In [15]:
df.ix[4] # Seleccionar un index
Out[15]:
In [16]:
df.ix[0::2] # Seleccionar índice por slice de dos en dos
Out[16]:
In [17]:
df.ix[0:5,'X'] # Seleccionar slice los índices de 0 a 5, la columna 'X'
Out[17]:
In [18]:
df.ix[0:5,['X','W']] # Seleccionar slice los índices de 0 a 5, multiples columnas
Out[18]: