2016-10-21 3 views
2
CD_FARE MTH DAY ID_CALENDAR H0 H1 H2 H3 PE1 PE2 PE3 PE4 
2.0  1 M Cal01  1  2 1 3 0.14 0.15 0.1 0.2 
2.0  1 T Cal01  1  2 1 3 0.14 0.16 0.1 0.2 
2.0  1 W Cal01  1  2 4 3 0.14 0.12 0.1 0.2 
2.0  1 T Cal01  2  2 1 3 0.14 0.11* 0.1 0.2 
2.0  1 F Cal01  4  2 1 3 0.14 0.18 0.1 0.2 

特定のセルからどのように値を取得することができますか。セルの値を行と列で取得する

たとえば、値0.11を返したいとします。 私は行(この場合は3)の位置と列(PE2)の名前を知っています。 は、私は明らかにそれが動作しません

data = df.iloc[3, 'PE2'] 

答えて

2

、それはとValueErrorに

ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types 

を与えるが、あなたの代わりにiloc方法df.loc[3, 'PE2']を使用している場合、それは

の作品?:この方法でデータを選択することができます
0

ポジションニードルで選択する必要がある場合Series.iloc

print (df['PE2'].iloc[3]) 
0.11 

サンプル:

df = pd.DataFrame({'PE2':[1,2,3], 
        'B':[4,5,6]}, index=['a','b','c']) 

print (df) 
    B PE2 
a 4 1 
b 5 2 
c 6 3 

#third row in colum PE2 
print (df['PE2'].iloc[2]) 
3 

#index value c and column PE2 
print (df.ix['c','PE2']) 
3 

#index value c and column PE2 
print (df.loc['c','PE2']) 
3 

#third row and second column 
print (df.iloc[2,1]) 
3 

が、インデックスと列の値を使用ixまたはDataFrame.locにより選択が必要な場合:

df = pd.DataFrame({'PE2':[1,2,3], 
        'B':[4,5,6]}) 

print (df) 
    B PE2 
0 4 1 
1 5 2 
2 6 3 

print (df.loc[2, 'PE2']) 
3 

print (df.ix[2, 'PE2']) 
3 

またpandas documentationselection by labelselection by positionを確認することができます。

関連する問題