我正在使用数据帧,将其分成两个数据帧,然后我需要更改索引值,以便没有数字大于总行数。
以下是代码:
dataset = pd.read_csv("dataset.csv",usecols['row_id','x','y','time'],index_col=0)
splitvalue = math.floor((0.9)*786239)
train = dataset[dataset.time < splitvalue]
test = dataset[dataset.time >= splitvalue]
这是我正在做的改变。我想知道是否有更简单的方法:
test.index=range(test.shape[0])
test.index.rename('row_id',inplace=True)
有更好的方法吗?
答案 0 :(得分:3)
尝试:
test = test.reset_index(drop=True).rename_axis('row_id')
答案 1 :(得分:2)
你应该在切片前随机播放数据......
dataset.reindex(np.random.permutation(dataset.index))
否则你的偏见你的测试/训练集。
答案 2 :(得分:2)
您可以直接指定新的Index
对象来覆盖索引:
test.index = pd.Index(np.arange(len(df)), name='row_id')