2015-10-08 9 views
5

このスレッドの拡張子である質問をします:Pandas DataFrameからシングルセル値を返します

Select rows from a DataFrame based on values in a column in pandas私は、単一の「値」ではないを返すことができる方法を、上記コードに基づい

A B  C D 
7 foo three 7 14 

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]) 

この次の結果をもたらす:

このスレッドからのコードを以下に示します行。つまり、行全体とは対照的に、値'7'または値'foo'を返すにはどうすればよいですか?

+1

df.loc == [] 'D' [DF [colNameに]またはdf.loc [== 14] 'D' [DF] 14] .values [index] –

答えて

5

@JonahWilliamsが近くにあった、ここで働く一つだ:

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

すばらしい。あなたのおかげです。 – Nick

関連する問題