Method chaining in Pandas

  Рет қаралды 1,964

Python and Pandas with Reuven Lerner

Python and Pandas with Reuven Lerner

7 ай бұрын

What is method chaining in Pandas? How does it work, and how can you use it?
In this video, I introduce method chaining, including the use of .loc, lambda, and assign. If you've been confused by any of these topics, you should get a better understanding from here.

Пікірлер: 26
@joaquinurruti2628
@joaquinurruti2628 6 күн бұрын
I come back to this video again and again every few months to recall, It's gold powder
@ReuvenLerner
@ReuvenLerner 6 күн бұрын
I'm so delighted to hear it; thanks for the warm and kind feedback.
@jingangmiao8367
@jingangmiao8367 6 ай бұрын
Instead of lambda functions, I find .query a lot more readable: ( df .assign(...) .query('amount_per_mile > 5') )
@ReuvenLerner
@ReuvenLerner 6 ай бұрын
I've never really gotten into using query, even though I know that it can have some performance benefits. But it can also fit nicely into this kind of chain.
@VladPalacios
@VladPalacios 23 күн бұрын
total newbie to Pandas here, I like this aproach, but seems to me, where it beraks the flow, is when you need to transform the data and keep it, .... like fill the na's, delete some columns... I'll try figure it out on my own and learn.... but really like the video
@ReuvenLerner
@ReuvenLerner 17 күн бұрын
Thanks for your kind remarks; it definitely takes time to get into the Pandas way of doing things. I've really enjoyed using method chaining in the last year or two, and I encourage you to try it before you knock it.
@VelkoKamenov
@VelkoKamenov 3 ай бұрын
Great content! This concept is especially useful for people coming to Python from R and the tidyverse syntax with the pipe operator (%>%) for chaining functions. About filtering rows how do you feel about using query() instead of loc?
@ReuvenLerner
@ReuvenLerner 3 ай бұрын
Thanks so much! I'm not a big fan of query, just because it introduces a totally new and different syntax -- it's sort of like embedding SQL inside of a program. That said, I admit that query can sometimes make things run faster, and can also look a bit clearer.
@taqial-shamiri3377
@taqial-shamiri3377 6 ай бұрын
Love the way you teach it and the examples.
@ReuvenLerner
@ReuvenLerner 6 ай бұрын
Thanks so much!
@onenabeable
@onenabeable 9 күн бұрын
thanks... clear explanation❤
@ReuvenLerner
@ReuvenLerner 6 күн бұрын
Thanks so much; happy it helped!
@horoshuhin
@horoshuhin 6 ай бұрын
same here. i got converted by Matt Harrison a few years back. and never looked back!
@vigneshkrishnan424
@vigneshkrishnan424 6 ай бұрын
The one thing I felt was missing coming to python from R background! Thankyou !
@ReuvenLerner
@ReuvenLerner 6 ай бұрын
I'm delighted to hear it!
@elu1
@elu1 6 ай бұрын
Simply brilliant! 👍
@ReuvenLerner
@ReuvenLerner 6 ай бұрын
Glad you enjoyed it!
@Lnd2345
@Lnd2345 6 ай бұрын
Great video. I think chaining is great and I use it but sometimes gets overused which makes those codes unreadable with zillions of lambda functions.
@ReuvenLerner
@ReuvenLerner 6 ай бұрын
Yes, indeed!
@imothar
@imothar 6 ай бұрын
I love method chaining as well, but do you know if the copy_on_write changes in pandas 3.0 will make it more difficult to use? Maybe I've just misunderstood the explanation 😅
@ReuvenLerner
@ReuvenLerner 6 ай бұрын
On the contrary, I just looked through the docs for 3.0 and copy on write, and it looks like this sort of chainig will definitely continue to work, whereas more direct assignment will not. When they talk about "chained assignment" not working, they're referring to something else, such as df['a'][100] = 200. That is already bad, and will stop working altogether.
@user-fn5rx3gp2t
@user-fn5rx3gp2t 6 ай бұрын
Hi I wanna ask you two questions where can I contact You?
@ReuvenLerner
@ReuvenLerner 6 ай бұрын
Feel free to e-mail me at reuven@lerner.co.il -- I'm not always able to answer individual questions, though.
@pietraderdetective8953
@pietraderdetective8953 6 ай бұрын
with all due respect to Harrison, his version of Pandas method chaining is too much overdone IMO...combined with his love for lambdas, it makes it even harder to read. too little whitespace, too many lines of codes getting cramped together...could be just his style of writing code which is perfectly fine for his use case. If you take a look at the profile of those who complained about this style, many of them writes much more difficult languages than Python...but they complained about the readability!
@ReuvenLerner
@ReuvenLerner 6 ай бұрын
Aha, got it. Like any tool or technique, it can be abused. I think of method chaining as a way to help clean up my code and my thinking. But if it makes things *more* complex, then that's not very helpful.
@RafsanSiddiqui77
@RafsanSiddiqui77 6 ай бұрын
Great one as always, Reuven! Apart from the readability of method chaining, is it better to use .loc to find total_amount > 100 and passenger_count > 5? I usually do it like this: df[(df['total_amount'] > 100) & (df['passenger_count' > 5)]. I would also do the amount_per_mile like that within [] without assign. Why shouldn't I do it that way?
The new case_when method in Pandas 2.2.0
10:07
Python and Pandas with Reuven Lerner
Рет қаралды 1,3 М.
What does Python's __init__ method do?
6:56
Python and Pandas with Reuven Lerner
Рет қаралды 32 М.
I'm Excited To see If Kelly Can Meet This Challenge!
00:16
Mini Katana
Рет қаралды 34 МЛН
Harley Quinn lost the Joker forever!!!#Harley Quinn #joker
00:19
Harley Quinn with the Joker
Рет қаралды 25 МЛН
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 100 МЛН
Pool Bed Prank By My Grandpa 😂 #funny
00:47
SKITS
Рет қаралды 18 МЛН
Selecting rows in Pandas using .loc and lambda
9:04
Python and Pandas with Reuven Lerner
Рет қаралды 996
Boolean indexing in Pandas made simple
8:23
Python and Pandas with Reuven Lerner
Рет қаралды 1,8 М.
What are the "nlargest" and "nsmallest" methods in Pandas? (And should you use them?)
8:06
Python and Pandas with Reuven Lerner
Рет қаралды 285
Speed Up Your Pandas Dataframes
11:15
Rob Mulla
Рет қаралды 70 М.
Compilers, How They Work, And Writing Them From Scratch
23:53
Adam McDaniel
Рет қаралды 145 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
25 Nooby Pandas Coding Mistakes You Should NEVER make.
11:30
Rob Mulla
Рет қаралды 266 М.
The Clever Way to Count Tanks - Numberphile
16:45
Numberphile
Рет қаралды 928 М.
Flipping Data with Pandas: Stack & Unstack
8:17
Python and Pandas with Reuven Lerner
Рет қаралды 2,6 М.
Kumanda İle Bilgisayarı Yönetmek #shorts
0:29
Osman Kabadayı
Рет қаралды 4 МЛН
Nokia 3310 top
0:20
YT 𝒯𝒾𝓂𝓉𝒾𝓀
Рет қаралды 5 МЛН
КОМП-ПЫЛЕСОС
1:00
VA-PC
Рет қаралды 731 М.
Data recovery from MicroSD using PC3000 Flash & Spider Board 😎
1:01
ГОТОВЫЙ ПК с OZON за 5000 рублей
20:24
Ремонтяш
Рет қаралды 283 М.