Aggregating values in one column by their corresponding value in another from two files

pythonuser890

had a question regarding summing the multiple values of duplicate keys into one key with the aggregate total. For example: 1:5 2:4 3:2 1:4 Very basic but I'm looking for an output that looks like: 1:9 2:4 3:2

In the two files I am using, I am dealing with a list of 51 users(column 1 of user_artists.dat) who have the artistID(column 2) and how many times that user has listened to that particular artist given by the weight(column 3).

I am attempting to aggregate the total times that artist has been played, across all users and display it in a format such as: Britney Spears (289) 2393140. Any help or input would be so appreciated.

import codecs
#from collections import defaultdict

with codecs.open("artists.dat", encoding = "utf-8") as f:
    artists = f.readlines()


with codecs.open("user_artists.dat", encoding = "utf-8") as f:
    users = f.readlines()


artist_list = [x.strip().split('\t') for x in artists][1:]
user_stats_list = [x.strip().split('\t') for x in users][1:]

artists = {}
for a in artist_list:
    artistID, name = a[0], a[1]
    artists[artistID] = name

grouped_user_stats = {}
for u in user_stats_list:
    userID, artistID, weight = u
    grouped_user_stats[artistID] = grouped_user_stats[artistID].astype(int)
    grouped_user_stats[weight] = grouped_user_stats[weight].astype(int)
    for artistID, weight in u:
        grouped_user_stats.groupby('artistID')['weight'].sum()
        print(grouped_user_stats.groupby('artistID')['weight'].sum())



    #if userID not in grouped_user_stats:
        #grouped_user_stats[userID] = { artistID: {'name': artists[artistID], 'plays': 1} }
    #else:
        #if artistID not in grouped_user_stats[userID]:
            #grouped_user_stats[userID][artistID] = {'name': artists[artistID], 'plays': 1}
        #else:
            #grouped_user_stats[userID][artistID]['plays'] += 1
            #print('this never happens') 




#print(grouped_user_stats)
Lohmar ASHAR

how about:

import codecs
from collections import defaultdict
# read stuff
with codecs.open("artists.dat", encoding = "utf-8") as f:
    artists = f.readlines()
with codecs.open("user_artists.dat", encoding = "utf-8") as f:
    users = f.readlines()
# transform artist data in a dict with "artist id" as key and "artist name" as value
artist_repo = dict(x.strip().split('\t')[:2] for x in artists[1:])

user_stats_list = [x.strip().split('\t') for x in users][1:]

grouped_user_stats = defaultdict(lambda:0)

for u in user_stats_list:
    #userID, artistID, weight = u
    grouped_user_stats[u[0]] += int(u[2]) # accumulate weights in a dict with artist id as key and sum of wights as values
# extra: "fancying" the data transforming the keys of the dict in "<artist name> (artist id)" format 
grouped_user_stats = dict(("%s (%s)" % (artist_repo.get(k,"Unknown artist"), k), v) for k ,v in grouped_user_stats.iteritems() )
# lastly print it
for k, v in grouped_user_stats.iteritems():
   print k,v 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Excel - Find unique values in one column and corresponding values from another

From Dev

How to compare multiple columns in two files and retrieve the corresponding value from another column if match found

From Dev

Finding min & max value from column one column and it's corresponding value from another

From Dev

How to get every value from one column to another column based on its corresponding value

From Dev

R: return a value from one column in a data frame corresponding to the minimum value in another column

From Dev

Replacing an empty cell in one column based on corresponding value from another column?

From Dev

R, subtract a value from the corresponding and following values in a numerical column each time conditions are met in another column

From Dev

Pandas - lambda - values in list and corresponding value from another column where values in list

From Dev

Use id values from one query, to corresponding column with same id in another table

From Dev

obtain corresponding value of one column based on minimum value of another in R

From Dev

if value lies between two values then add another value to corresponding line

From Dev

Replace value in column with corresponding value from another column in same dataframe

From Dev

How to subtract cell values from one column with cell values from another column in xlsx files using python

From Dev

MySQL query to find if a value of one column in one table is between two values in two columns on another table

From Dev

Aggregating one matrix by values in another matrix

From Dev

Compare 2 files with awk and substract value from one column if the value of another column matches in both files

From Dev

Find value in one column and print corresponding value from other one

From Dev

SQL subquery to return MIN of a column and corresponding values from another column

From Dev

Pandas: select from column with index corresponding to values in another column

From Dev

Select MAX value from column and corresponding value from another

From Dev

How to check value of a column lies between values of two columns in other file and print corresponding value from column in Unix?

From Dev

compare whether value in one column is between two values in another column python pandas

From Dev

MySQL Count values from one column with selecting a value from another column

From Dev

SQL Query to compare two columns with one column from another table (and get two values)

From Dev

adding values from one column of a data frame into a new column of another dataframe if the first two columns in both match

From Dev

Finding median of values in one column given corresponding consecutive values in another column are equal numbers

From Dev

Set all values in one column to NaN if the corresponding values in another column are also NaN

From Dev

How to replace ID in one table with corresponding value from another table?

From Dev

Joing two tables showing me records from one tables where it doesn't have a corresponding value in another table

Related Related

  1. 1

    Excel - Find unique values in one column and corresponding values from another

  2. 2

    How to compare multiple columns in two files and retrieve the corresponding value from another column if match found

  3. 3

    Finding min & max value from column one column and it's corresponding value from another

  4. 4

    How to get every value from one column to another column based on its corresponding value

  5. 5

    R: return a value from one column in a data frame corresponding to the minimum value in another column

  6. 6

    Replacing an empty cell in one column based on corresponding value from another column?

  7. 7

    R, subtract a value from the corresponding and following values in a numerical column each time conditions are met in another column

  8. 8

    Pandas - lambda - values in list and corresponding value from another column where values in list

  9. 9

    Use id values from one query, to corresponding column with same id in another table

  10. 10

    obtain corresponding value of one column based on minimum value of another in R

  11. 11

    if value lies between two values then add another value to corresponding line

  12. 12

    Replace value in column with corresponding value from another column in same dataframe

  13. 13

    How to subtract cell values from one column with cell values from another column in xlsx files using python

  14. 14

    MySQL query to find if a value of one column in one table is between two values in two columns on another table

  15. 15

    Aggregating one matrix by values in another matrix

  16. 16

    Compare 2 files with awk and substract value from one column if the value of another column matches in both files

  17. 17

    Find value in one column and print corresponding value from other one

  18. 18

    SQL subquery to return MIN of a column and corresponding values from another column

  19. 19

    Pandas: select from column with index corresponding to values in another column

  20. 20

    Select MAX value from column and corresponding value from another

  21. 21

    How to check value of a column lies between values of two columns in other file and print corresponding value from column in Unix?

  22. 22

    compare whether value in one column is between two values in another column python pandas

  23. 23

    MySQL Count values from one column with selecting a value from another column

  24. 24

    SQL Query to compare two columns with one column from another table (and get two values)

  25. 25

    adding values from one column of a data frame into a new column of another dataframe if the first two columns in both match

  26. 26

    Finding median of values in one column given corresponding consecutive values in another column are equal numbers

  27. 27

    Set all values in one column to NaN if the corresponding values in another column are also NaN

  28. 28

    How to replace ID in one table with corresponding value from another table?

  29. 29

    Joing two tables showing me records from one tables where it doesn't have a corresponding value in another table

HotTag

Archive