在多个数据框上重置索引

时间:2019-10-15 19:06:48

标签: pandas dataframe

我想做的是编写一行代码,以重置某些数据帧上的索引。我以为我可以写这样的东西

X_train, X_test_, y_train, y_test = train_test_split(X,y)
map(lambda x: x.reset_index(drop=True,inplace=True),
                 [X_train, X_test_, y_train, y_test]) 

但是没有理想的结果。有建议吗?

1 个答案:

答案 0 :(得分:2)

map是一个惰性操作。直到您遍历地图,它才会运行。您只需运行列表理解即可达到所需的结果:

X_train, X_test_, y_train, y_test = [
    df.reset_index(drop=True)
    for df in train_test_split(X,y)
]