![](/img/trans.png)
[英]How to split the whole dataset into 4 range based on one column using python
[英]How to split the dataset in python based on the number of records
我想根据数字将数据框分为两个
train = corpus.iloc[:, :10000]
test = corpus.iloc[:, 10000:]
这是我正在使用的代码。
我收到以下错误:
AttributeError: iloc not found
iloc不是python3的一部分吗? 还有其他方法可以根据要拆分的记录数来拆分数据吗?
编辑如用户@craig所述,我loc是大熊猫,而我拥有的数据类型是稀疏矩阵(scipy.sparse.csr.csr_matrix)
不需要iloc
,可以直接使用行切片:
熊猫
import pandas as pd
df = pd.DataFrame(range(10))
df_first_half = df[:5]
df_second_half = df[5:]
SciPy的
import numpy as np
from scipy.sparse import csr_matrix
x = csr_matrix((10, 3), dtype=np.int8)
x_first_half = x[:5].toarray()
x_second_half = x[5:].toarray()
如果您不熟悉[5:]
表示法,请参见: https : //scipy-cookbook.readthedocs.io/items/Indexing.html 。 简而言之,它是一维切片(行)。 也可以使用多维切片,例如[5:,:1]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.