2013-07-01 8 views
8

Mam doświadczenie w R i nowe w Pandonie Pythona. Próbuję indeksować DataFrame w celu pobrania wierszy, które spełniają zestaw kilku warunków logicznych - podobnie jak "instrukcja" SQL.indeksu Pythona Pandas ramka danych z wieloma warunkami SQL, takie jak instrukcja gdzie

Wiem, jak to zrobić w R z ramkami danych (i pakietem R data.table, który jest bardziej podobny do Pandas DataFrame niż natywna ramka danych R).

Oto przykładowy kod, który tworzy obiekt DataFrame i opis tego, w jaki sposób chciałbym go zaindeksować. Czy istnieje prosty sposób na zrobienie tego?

import pandas as pd 
import numpy as np 

# generate some data 
mult = 10000 
fruits = ['Apple', 'Banana', 'Kiwi', 'Grape', 'Orange', 'Strawberry']*mult 
vegetables = ['Asparagus', 'Broccoli', 'Carrot', 'Lettuce', 'Rutabaga', 'Spinach']*mult 
animals = ['Dog', 'Cat', 'Bird', 'Fish', 'Lion', 'Mouse']*mult 
xValues = np.random.normal(loc=80, scale=2, size=6*mult) 
yValues = np.random.normal(loc=79, scale=2, size=6*mult) 

data = {'Fruit': fruits, 
     'Vegetable': vegetables, 
     'Animal': animals, 
     'xValue': xValues, 
     'yValue': yValues,} 

df = pd.DataFrame(data) 

# shuffle the columns to break structure of repeating fruits, vegetables, animals 
np.random.shuffle(df.Fruit) 
np.random.shuffle(df.Vegetable) 
np.random.shuffle(df.Animal) 

df.head(30) 

# filter sets 
fruitsInclude = ['Apple', 'Banana', 'Grape'] 
vegetablesExclude = ['Asparagus', 'Broccoli'] 

# subset1: All rows and columns where: 
# (fruit in fruitsInclude) AND (Vegetable not in vegetablesExlude) 

# subset2: All rows and columns where: 
# (fruit in fruitsInclude) AND [(Vegetable not in vegetablesExlude) OR (Animal == 'Dog')] 

# subset3: All rows and specific columns where above logical conditions are true. 

Cała pomoc i wejścia mile widziane i wysoko cenione!

Dzięki, Randall

+0

Wow. Dokładnie to, czego potrzebowałem. Dzięki za szybką i bezpośrednią odpowiedź. Zauważ, że pisałem warzywa. Wyróżnij źle ... powinny być warzywa Wyłącz (za pomocą c). Poprawiono to w powyższym kodzie, więc należy skopiować i wkleić do przetestowania. Dzięki jeszcze raz. Randall. – user2537610

Odpowiedz

14
# subset1: All rows and columns where: 
# (fruit in fruitsInclude) AND (Vegetable not in vegetablesExlude) 
df.ix[df['Fruit'].isin(fruitsInclude) & ~df['Vegetable'].isin(vegetablesExclude)] 

# subset2: All rows and columns where: 
# (fruit in fruitsInclude) AND [(Vegetable not in vegetablesExlude) OR (Animal == 'Dog')] 
df.ix[df['Fruit'].isin(fruitsInclude) & (~df['Vegetable'].isin(vegetablesExclude) | (df['Animal']=='Dog'))] 

# subset3: All rows and specific columns where above logical conditions are true. 
df.ix[df['Fruit'].isin(fruitsInclude) & ~df['Vegetable'].isin(vegetablesExclude) & (df['Animal']=='Dog')] 
+0

Ledwo mnie też bij! To było dokładnie to samo rozwiązanie, które wymyśliłem +1 – spencerlyon2

+0

Jeśli chciałem tylko indeksów, istnieje krótsza droga niż to: 'df.ix [df ['Fruit']. Isin (fruitsInclude) .index' – Rhubarb

+0

@ Zhubarb: 'df.index [df ['Fruit']. Isin (fruitsInclude)]' jest krótszy i (na moim komputerze ~ 33%) szybszy niż 'df.ix [df ['Fruit']. Isin (fruitsInclude)] .index'. – unutbu

Powiązane problemy