2015-10-08 18 views
5

Ich möchte eine Frage stellen, die eine Erweiterung auf diesem Thread ist:von Pandas Dataframe

Select rows from a DataFrame based on values in a column in pandas.

Der Code aus diesem Thread ist unten aufgeführt:

import pandas as pd 
import numpy as np 
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(), 
       'B': 'one one two three two two one three'.split(), 
       'C': np.arange(8), 'D': np.arange(8) * 2}) 
print(df) 
#  A  B C D 
# 0 foo one 0 0 
# 1 bar one 1 2 
# 2 foo two 2 4 
# 3 bar three 3 6 
# 4 foo two 4 8 
# 5 bar two 5 10 
# 6 foo one 6 12 
# 7 foo three 7 14 

print(df.loc[df['D'] == 14]) 

Dies wird das folgende Ergebnis erhalten wird:

A B  C D 
7 foo three 7 14 

Basierend auf dem obigen Code, wie kann ich einen einzigen ‚Wert‘ nicht wieder eine Reihe. Das heißt, wie kann ich den Wert '7' oder den Wert im Gegensatz zur gesamten Zeile zurückgeben?

+1

df.loc [df [ 'D'] == 14] [colName] oder df.loc [df [ 'D'] == 14] .Werte [Index] –

Antwort

5

@JonahWilliams war in der Nähe, hier eine Arbeit ein:

import pandas as pd 
import numpy as np 
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(), 
       'B': 'one one two three two two one three'.split(), 
       'C': np.arange(8), 'D': np.arange(8) * 2}) 

print(df.loc[df['D'] == 14]['A'].index.values) 

>>>[7] 

print(df.loc[df['D'] == 14]['A'].values) 

>>>['foo'] 
+0

Fantastisch. Vielen Dank an euch beide. – Nick