Search code examples
pythonpandasnumpydataframerecommendation-engine

access only first 80% columns of a data frame


I want to access only the first 80% columns of my dataframe and store it into new data frame whereas store the remainig 20% in another data frame. Here is something I tried:

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

however this gave an error:

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

ratings_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]

Solution

  • You should remove the brackets. You only need df.shape[1]. By the way for more readability, I suggest you use rather

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

    Or something like that