python - 仅访问数据框的前 80% 列
问题描述
我只想访问我的数据帧的前 80% 列并将其存储到新数据帧中,而将剩余的 20% 存储在另一个数据帧中。这是我尝试过的东西:
ratings_df=ratings_df.iloc[:,:int(ratings_df.shape()[1]*0.8)-1]
然而这给出了一个错误:
Traceback (most recent call last):
File "S:\TIP\Code\MF_research.py", line 15, in <module>
ratins_df=ratings_df.iloc[:,:int(ratings_df.shape()[1]*0.8)-1]
TypeError: 'tuple' object is not callable
评分_df:
MovieID 1 2 3 4 5 6 ... 3947 3948 3949 3950 3951 3952
UserID ...
1 5.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0
3 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0
4 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0
5 0.0 0.0 0.0 0.0 0.0 2.0 ... 0.0 0.0 0.0 0.0 0.0 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ...
6036 0.0 0.0 0.0 2.0 0.0 3.0 ... 0.0 0.0 0.0 0.0 0.0 0.0
6037 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0
6038 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0
6039 0.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0
6040 3.0 0.0 0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0 0.0 0.0
[6040 rows x 3706 columns]
解决方案
您应该删除括号。你只需要 df.shape[1]。顺便说一下,为了提高可读性,我建议您使用
shape_80 = int(df.shape[1]*0.8)-1
ratings_df=ratings_df.iloc[:,:shape_80]
或类似的东西
推荐阅读
- networking - 如何让传感器/海龟运行功能优先考虑当前最高能量的传感器/海龟?网标
- nestjs - 如何使用 { scope: Scope.REQUEST } 对服务上的每个请求调用方法?
- python - 熊猫:无法根据其他列的条件设置单元格值
- php - 在循环中渲染树枝组件
- c++ - Code Chef DSA 学习系列 : Multiple of 3 Multhree
- postgresql - 将 hasura 连接到现有的 postgresql
- python - 安装 scikit-image 时如何解决此错误?
- postgresql - On any of my Heroku apps, several `heroku pg:` commands fail immediately with "Cannot read property 'includes' of undefined."
- php - change the final number of the permalink by +1 php
- python - Low accuracy for triplet loss in image recognition