Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Pandas: create summary table

In a python pandas dataframe "df", I have the following columns:

user_id | song_id | song_duration | song_title | artist | listen_count

Many users might have listened to the same song - therefore the song is not unique in this table. I would like to create a second dataframe with just song information (with unique song_ids).

song_id | song_title | artist

I manage to create a table with song_id and song_title.

song_df = df.groupby('song_id').song_title.first()

How can I add, the column "artist" into this?

This doesn't work:

song_df = df.groupby('song_id').df['song_title','artist'].first()

AttributeError: 'DataFrameGroupBy' object has no attribute 'df'

like image 392
jeangelj Avatar asked Sep 26 '26 07:09

jeangelj


1 Answers

IIUC try omit .df:

df.groupby('song_id')['song_title','artist'].first()
like image 156
jezrael Avatar answered Sep 27 '26 19:09

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!