Python 按索引拆分:我想拆分火车+;根据已给出指标的数据进行测试。我如何获得列车/测试df?

Python 按索引拆分:我想拆分火车+;根据已给出指标的数据进行测试。我如何获得列车/测试df?,python,pandas,dataframe,Python,Pandas,Dataframe,例如=df是具有特征的数据。我想从已给出指标的数据中分离列车+测试。我如何获得列车/测试df df= 0 2 0.3 0.5 0.5 1 4 0.5 0.7 0.4 2 2 0.5 0.1 0.4 3 4 0.4 0.1 0.3 4 2 0.3 0.1 0.5 train.txt在哪里 train=pd.read_csv(data_train.txt) 其中,在此数据帧中给出了索引。我应该如何从这些指数中获得训练数据 data_train.txt中的内容(有10000个数据,其中该txt文件

例如=df是具有特征的数据。我想从已给出指标的数据中分离列车+测试。我如何获得列车/测试df

df=
0 2 0.3 0.5 0.5
1 4 0.5 0.7 0.4
2 2 0.5 0.1 0.4
3 4 0.4 0.1 0.3
4 2 0.3 0.1 0.5
train.txt在哪里

train=pd.read_csv(data_train.txt)
其中,在此数据帧中给出了索引。我应该如何从这些指数中获得训练数据

data_train.txt中的内容(有10000个数据,其中该txt文件中给出了列车索引)

我希望这些指标的训练数据与功能:-喜欢

最终列车应如下所示(见索引):


如果您的df由以下人员给出:

   0  1    2    3    4
0  0  2  0.3  0.5  0.5
1  1  4  0.5  0.7  0.4
2  2  2  0.5  0.1  0.4
3  3  4  0.4  0.1  0.3
4  4  2  0.3  0.1  0.5
   0
0  0
1  2
2  4
以及另一个列_指数,如下所示:

   0  1    2    3    4
0  0  2  0.3  0.5  0.5
1  1  4  0.5  0.7  0.4
2  2  2  0.5  0.1  0.4
3  3  4  0.4  0.1  0.3
4  4  2  0.3  0.1  0.5
   0
0  0
1  2
2  4
然后,您需要做的所有工作都是获取
df
的对应行,这取决于数据的组织方式:

#if you're trying to match the index of the df itself
train_df = df.iloc[train_indices]
#if you're trying to match column 0, which might be important 
#if it's not aligned to the index
train_df =  df.loc[df[0].isin(train_indices)]
这两种(在本例中)都返回:


我的意思是我尝试了其他不同的方法-``X,Y=sklearn.datasets.load_svmlight_file(Data_xt')train_id=any txt read函数('Data-train-index.txt')trainX=X[train_id,:]trainY=Y[train_id,:]--它说它需要是整数,解决方案是什么```我将data-train-indexs.txt作为一个csv文件读取,它将它们默认为整数而不是字符串。要解决这个问题,请调用
index=np.array(index).astype(int)
(假设您的索引被称为“index”。谢谢,我会尝试这个!还有您提供的最后一件事,为什么它有第1列?我想它不应该在那里,您能澄清一下吗?第1列(2,2,2),我的意思是,我只需要我的索引来提取数据,因此列车具有所有索引行的特征(0,2,4,10,59…,103…10000)那是因为我读到了你们按原样给出的df,这意味着我把它作为一个专栏来读。如果它不在你们的数据框架中,那么它在你们索引之后就不会出现了。啊,我明白了!谢谢,如果我有任何问题,我会尝试发表评论。但请回复:-)欢迎这样做;请花一分钟时间看看如何正确格式化问题中的代码和文本(这次是为您完成的)。
   0  1    2    3    4
0  0  2  0.3  0.5  0.5
2  2  2  0.5  0.1  0.4
4  4  2  0.3  0.1  0.5