2

I have dataframe which has 5 columns.

Df = pd.DataFrame(columns=['userId','movieId','rating','timestamp','genres'])

I have this function

def Main():

    flag = 0
    while(flag <=99):
        data1 = pickRandomMovies()
        data2 = usersToMovies(data1)
        if len(data2) < 6:
            data2 = data1
        else:
            flag +=1
            data3 = commonUsers(data2)
            #append all values of data3 here to new dataframe
            df_new.append(data3)

            findingRatings(data3)
>>data3
userId  movieId  rating   timestamp                      genres
 27       60      3.0     962685387         Adventure|Children|Fantasy
 103      60      4.0     1431968436        Adventure|Children|Fantasy
 160      60      2.0     971619579         Adventure|Children|Fantasy
 27      1210     4.0     962684909         Action|Adventure|Sci-Fi
 103     1210     3.5     1431957028        Action|Adventure|Sci-Fi
 160     1210     5.0     971113953         Action|Adventure|Sci-Fi

i want to append all values of data3 into new dataframe in every iteration.

7
  • 1
    you can initialize a new dataframe e.g. df_new = pd.DataFrame(columns=['whatever']) and then you can use df_new.append(data3). If the collumns are the same then this will be okay. Commented Dec 3, 2019 at 8:44
  • How does the data in data3 looks like? Could you print out data3 so its easier to see? Commented Dec 3, 2019 at 8:49
  • @geoph9 i have tried this but new dataframe comes empty Commented Dec 3, 2019 at 8:59
  • @Jan33 data3 is printed. Commented Dec 3, 2019 at 9:06
  • can you share the code you have tried for which you got empty dataframe Commented Dec 3, 2019 at 9:12

1 Answer 1

1

change your code to

def Main():

    flag = 0
    while(flag <=99):
        data1 = pickRandomMovies()
        data2 = usersToMovies(data1)
        if len(data2) < 6:
            data2 = data1
        else:
            flag +=1
            data3 = commonUsers(data2)
            #append all values of data3 here to new dataframe
            df_new=df_new.append(data3)

            findingRatings(data3)

df_new.append(data3) only appends the value it does not assign the value it should be done explicitly by df_new=df_new.append(data3)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.