python索引与切片_在Python中对一系列进行切片和索引

我正在学习Python和Scikit learn,我正在做一些简单的练习。在特定情况下,我运行以下代码: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])

我打印:

^{pr2}$

输出: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系列测试的第一个元素: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]

出去KeyError: 2L

怎么回事?当我对前5个元素序列进行切片时,以及在分别索引该序列的每个元素时,为什么会返回不同的值?为什么我在索引序列的3d元素时会收到关键错误消息?在

你的建议将不胜感激

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值