0

I have this Youtube channels dataset from kaggle wherein i want to create a new dataframe which has category_name as music only. here's the original dataframe--

df = pd.read_csv('channels.csv')
df.head(3)

the output for this is

category_id category_name   channel_id  country description followers   join_date   location    picture_url profile_url title   trailer_title   trailer_url videos
0   24  Entertainment   UC-lHJZR3Gqxm24_Vd_AJ5Yw    United States   I make videos.  69896406    2010-04-29  NaN https://yt3.ggpht.com/a-/AN66SAztY6oYWZnS1Cae9...   http://www.youtube.com/channel/UC-lHJZR3Gqxm24...   PewDiePie   bitch lasagna   https://www.youtube.com/watch?v=6Dh-RL__uN4 3649
1   10  Music   UCq-Fj5jknLsUf-MWSy4_brA    India   \   69471946    2006-03-13  NaN https://yt3.ggpht.com/a-/AN66SAxPfKnfHAnAs0rOq...   http://www.youtube.com/channel/UCq-Fj5jknLsUf-...   T-Series    Guru Randhawa: GOLIMAAR Lyrical Video | Bhusha...   https://www.youtube.com/watch?v=nJ1f44JvlC8 12820
2   24  Entertainment   UCIwFjwMjI0y7PDBVEO9-bkQ    NaN Help change the world. OUR album \'Purpose\' o...   41858494    2007-01-15  NaN https://yt3.ggpht.com/a-/AN66SAzY-4LlEPxP9YOux...   http://www.youtube.com/channel/UCIwFjwMjI0y7PD...   Justin Bieber   DJ Khaled - No Brainer (Official Video) ft. Ju...   https://www.youtube.com/watch?v=kxloC1MKTpg 132

I wanted to create a new dataframe wherein I only want to store the values having category_name = Music. I've tried using the groupby and apply functions however didn't get the desired result

1 Answer 1

1

You might want to use boolean slicing.

is_music = df['category_name'] == 'Music'
new_df = df[is_music]

This works because is_music is a boolean array that indicates whether a the category_name column of a row equals Music. In the three-row example you have above, is_music would look something like

>>> is_music
0    False
1    True
2    False

Then, we can use this information to select only the rows of the data frame where is_music is True. This is what the second line of code is achieving. Of course, you could just write all of this in one line as well:

new_df = df[df['category_name'] == 'Music']
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.