首页 > 解决方案 > 当数据框只有列时,shape命令不返回列号

问题描述

数据集如下所述。定义变量是考试。考试形状 = (29,2)

    |Hours| |Pass| 
0   |0.5 |    |0|  
1   |0.75|    |0|  
2   |1.00|    |0|  
3   |1.25|    |0|  
4   |1.50|    |0| 

截图中附有清晰的图像

数据集图片 - 考试

X = exam.Hours  
y = exam.Pass 

X.shape = (29,)  # The column number one is not mentioned   
y.shape = (29,) # The column value is not mentioned 

预期结果

X.shape = (29,1)    
y.shape = (29,1)

标签: pythonpandasdataframenumpydataset

解决方案


两个数组都是一维的,您希望添加一个额外的维度。您需要在阵列上取消挤压新轴。

>>> x = np.random.rand(29)

>>> x.shape
(29,)

要么使用索引:

>>> x = x[..., np.newaxis] # i.e. x[..., None]

>>> x.shape
(29, 1)

或使用np.expand_dims实用程序:

>>> x = np.expand_dims(x, -1)

>>> x.shape
(29, 1)

推荐阅读