首页 > 解决方案 > 仅访问数据框的前 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]

标签: pythonpandasnumpydataframerecommender-systems

解决方案


您应该删除括号。你只需要 df.shape[1]。顺便说一下,为了提高可读性,我建议您使用

shape_80 = int(df.shape[1]*0.8)-1
ratings_df=ratings_df.iloc[:,:shape_80]

或类似的东西


推荐阅读