在尝试使用python和pandas过滤一些过去的数据时遇到类型错误。这是错误
TypeError:无法在<上执行切片停止值索引类 'pandas.core.index.Int64Index' >这些索引器[327.0]的<类型 '浮动' >
代码
# 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列。
那么大家,对此有何想法?
答案 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)]
您的代码应该是什么样的
train_data_df = df_replace[:int(dataset_length*ratio)]
test_data_df = df_replace[-(1-int(dataset_length*ratio)):]