0

I have this code and i want to merge results of v in finding_rating function into one list. The type of result coming from v is <class 'numpy.float64'>.



from pandas import DataFrame
import pandas as pd
import numpy as np
from itertools import chain

negative_counter = []
positive_counter = []

df = pd.DataFrame({'userId': [10,20,10,20,10,20,60,90,60,90,60,90,30,40,30,40,30,40,50,60,50,60,50,60],
                   'movieId': [500,500,800,800,700,700,1100,1100,1900,1900,2000,2000,1600,1600,1901,1901,3000,3000,3025,3025,4000,4000,500,500],  
                   'ratings': [3.5,4.5,2.0,5.0,4.0,1.5,3.5,4.5,3.5,4.5,2.0,5.0,4.0,1.5,3.5,4.5,3.5,4.5,2.0,5.0,4.0,1.5,3.5,4.5]})

counter = [] 


numberOfUsers = 2
numberOfMovies = 3
usersLength = numberOfUsers*numberOfMovies
total_length = 6 

def grouping_data(df):
    # df_new = pd.DataFrame(columns=['genres','movieId','rating','timestamp', 'title', 'userId', 'year'])
    data = df[df.index % total_length < usersLength]
    return data
# grouping_data(df)

def chunker(seq, size):
    return (seq[pos:pos + size] for pos in range(0, len(seq), size))



def finding_rating(data):
     for chunk in chunker(data,usersLength):
        r = chunk.pivot(index="movieId",columns="userId")
        r.columns = ["u1","u2"]                                                                                                

        r["drate"] = r.u1.sub(r.u2).abs()
        v = r.drate.iloc[:-1].mean()-r.drate.iloc[-1]
        print(type(v))

data1 = grouping_data(df)
data2 = finding_rating(data1)
Sanwal
  • 307
  • 2
  • 11
  • Have you tried appending `v` to a list then returning the list? – wwii Dec 31 '19 at 15:26
  • @wwii I have given the answer below. – Sanwal Dec 31 '19 at 15:27
  • @Datanovice Actually, i wanted to find positive and negative values coming from v. So, i simply appended positive and negative values into different list. – Sanwal Dec 31 '19 at 15:38
  • @wwii It's kind of same concept but i wanted to find positive and negative values coming from v. So, i simply appended positive and negative values into different list. – Sanwal Dec 31 '19 at 15:39

1 Answers1

0

Add these lines after v

if v<0:
      negative_counter.append(v)
else:
      positive_counter.append(v)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sanwal
  • 307
  • 2
  • 11
  • did you mean to add this as an answer? – Umar.H Dec 31 '19 at 15:27
  • @Datanovice Yes, this is the right answer. When i publised the question i wasn't able to think in this way. After publishing i solved it. – Sanwal Dec 31 '19 at 15:30
  • The example in the question does not have either `negative_counter` or `positive_counter` - this would produce a `NameError` – wwii Dec 31 '19 at 15:30
  • @wwii if anything, this is just appending to a list which probably has 100's of answers. Best to list as duplicate to close. – Umar.H Dec 31 '19 at 15:30
  • @Datanovice find a dupe and vote. – wwii Dec 31 '19 at 15:31