简体   繁体   English

在 Python 中对系列进行切片和索引

[英]Slicing and Indexing a Series in Python

I am learning Python and Scikit learn and I am doing some simple exercises.我正在学习 Python 和 Scikit 学习,我正在做一些简单的练习。 In the particular case I run the following code:在特定情况下,我运行以下代码:

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

I print:我打印:

print(X_test_raw[0:5])

output:输出:

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

Then I am indexing one by one the first elements of the Series X_test_raw:然后我将 X_test_raw 系列的第一个元素一个一个地编入索引:

X_test_raw[0]

out出去

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

then然后

X_test_raw[1]

out出去

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

then然后

X_test_raw[2]

out出去

KeyError: 2L

What is going on?到底是怎么回事? Why I get different values returned when I am slicing the first 5 elements sequence and when I am indexing each element of this sequence separately?为什么当我对前 5 个元素序列进行切片时以及当我分别索引该序列的每个元素时会返回不同的值? Why I get a key error message when I am indexing the 3d element of the Series?为什么在索引系列的 3d 元素时会收到关键错误消息?

Your advice will be appreciated您的建议将不胜感激

If use X_test_raw[2] you try get row with index=2 , but if missing get:如果使用X_test_raw[2]您尝试使用index=2获取row ,但如果缺少获取:

KeyError: 2L密钥错误:2L

For select by position need iloc or iat :按位置选择需要ilociat

X_test_raw.iloc[2]

Sample:样本:

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

You can check:您可以检查:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM