Python TypeError:无法对<class'pandas.core.index.int64index'=“”>执行切片停止值索引

时间:2016-10-26 16:05:28

标签: python python-2.7 csv pandas

在尝试使用python和pandas过滤一些过去的数据时遇到类型错误。这是错误

  

TypeError:无法在&lt;上执行切片停止值索引类   'pandas.core.index.Int64Index' &GT;这些索引器[327.0]的&lt;类型   '浮动' &GT;

代码

# 65% of training data
ratio = 0.65
train_data_df = df_replace[:round(dataset_length*ratio)]  
test_data_df = df_replace[-(1-round(dataset_length*ratio)):]  

# Create Respected CSV
train_data_df.to_csv('Train.csv',index=False)
test_data_df.to_csv('Test.csv',index=False)

其他信息

代码正在制作新的CSV文件India_in_Tests_Filter.csv,其中包含450多行和3列,如下所示:

Result Toss Bat

Lost   won  1st 
Won    won  2nd

虽然India_in_Tests.csv有超过450行和7列。

那么大家,对此有何想法?

1 个答案:

答案 0 :(得分:3)

考虑df

df = pd.DataFrame(range(10), list(range(320, 330)))

然后用

切片
df[:327.0]
TypeError: cannot do slice indexing on <class 'pandas.indexes.numeric.Int64Index'>
with these indexers [327.0] of <type 'float'>

您的round函数返回float。改为int

df[:int(327.0)]

enter image description here

您的代码应该是什么样的

train_data_df = df_replace[:int(dataset_length*ratio)]  
test_data_df = df_replace[-(1-int(dataset_length*ratio)):]