J
J
Joseph Goodman2020-05-24 12:51:30
Python
Joseph Goodman, 2020-05-24 12:51:30

What is the difference between these pieces of code?

Good day. I am studying the Pandas library in Yandex.Practice and ran into an incident.
The validator passes one solution, but not the other. I can not understand in connection with what, because the conclusion they seem to have is the same at first glance.
The first option works, but the second one doesn't.
Maybe they have something else difference, but I can not understand it?

import pandas as pd
df = pd.read_csv('music_log_upd.csv')

#1 вариант
pop_music = df[(df['total_play_seconds']!=0)&(df['genre_name']=='pop')]
print(pop_music)

#2 вариант
pop_music = df[(df.loc[:, 'total_play_seconds']!=0)&(df.loc[:, 'genre_name']=='pop')]
print(pop_music)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nazar Tropanets, 2020-05-24
@nazartropanets

loc is used to fetch an element(s) by condition, you can't put loc in df[] because loc must return a new dataframe(or Series). Use the first option or connect two different locs.
I hope I explained clearly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question