2017-02-04 12 views
1

私はPythonとScikitを学びます。私はいくつかの簡単な練習をしています。特定のケースでは、私は次のコードを実行します。Pythonでのシリーズのスライシングとインデックス付け

import pandas as pd 
df = pd.read_csv('SMSSpamCollection',delimiter='\t',header=None) # from UCIMachineLearningRepository http://archive.ics.uci.edu/ml/datasets/SMS+Spam+Collection 
import numpy as np 
from sklearn.feature_extraction.text import TfidfVectorizer 
from sklearn.linear_model.logistic import LogisticRegression 
from sklearn.cross_validation import train_test_split, cross_val_score 
X_train_raw, X_test_raw, y_train, y_test = train_test_split(df[1], df[0]) 

を私が印刷:

print(X_test_raw[0:5]) 

出力:シリーズX_test_rawのその後

3035  Get ready for <#> inches of pleasure... 
2577     In sch but neva mind u eat 1st lor.. 
3302    RCT' THNQ Adrian for U text. Rgds Vatian 
90  Yeah do! Don‘t stand to close tho- you‘ll catc... 
2355     R we going with the <#> bus? 
Name: 1, dtype: object 

私はインデックスを作成しています一つずつ最初の要素を:

X_test_raw[0] 

次いで

'Go until jurong point, crazy.. Available only in bugis n great world la e buffet... Cine there got amore wat...' 

X_test_raw[1] 

'Ok lar... Joking wif u oni...' 

アウト次いで

X_test_raw[2] 

アウト

アウト

何が起こっているのですか?最初の5要素のシーケンスをスライスしているときと、このシーケンスの各要素を別々にインデックスしているときに、異なる値が返されるのはなぜですか?シリーズの3D要素のインデックスを作成するときにキーエラーメッセージが表示されるのはなぜですか?

あなたのアドバイスは、使用がX_test_raw[2]あなたがindex=2rowを取得しようとした場合

答えて

1

を高く評価しますが、GET欠落している場合されます。位置によって選択について

KeyError: 2L

が必要ilociat

X_test_raw.iloc[2] 

サンプル:

s = pd.Series(['a','s','f'], index=[2,3,5]) 
print (s) 
2 a 
3 s 
5 f 
dtype: object 

print (s[2]) 
a 

print (s[1:3]) 
3 s 
5 f 
dtype: object 

print (s.loc[2]) 
a 


print (s.iloc[2]) 
f 

次のことが確認できます。

関連する問題