Comparing values in Pandas with "diff" and "pct_change"

  Рет қаралды 786

Python and Pandas with Reuven Lerner

Python and Pandas with Reuven Lerner

Күн бұрын

Want to know how much the values in a series or data frame have changed from row to row? Meet the "diff" method, which will calculate this for you -- either from the previous row, or from any other row. And if you want to know the percentage change? Just use "pct_change" instead.

Пікірлер: 4
@AndyWallWasWeak
@AndyWallWasWeak 6 ай бұрын
off topic Q, but inspired by a moment from this vid. When writing own functions, should I write for pd.Series or 1-column DataFrame? Sounds like could be multiple things to keep in mind when deciding
@ReuvenLerner
@ReuvenLerner 6 ай бұрын
I'd suggest not writing anything for a 1-column data frame. Either write for a data frame (regardless of size), or for a series / column. I think that the latter is probably a better way to go, overall.
@tyl9680
@tyl9680 4 ай бұрын
What about diff by different categories? Say I have corn, rice, beans and wheat prices in the same df, and I want to compare the price changes within the same catogories.
@ReuvenLerner
@ReuvenLerner 4 ай бұрын
You can totally do this! Just use "diff" on the result of a "groupby". For example: df = DataFrame({'category': ['wheat', 'corn', 'rice', 'wheat', 'corn', 'rice', 'wheat', 'corn', 'rice'], 'price': [10, 8, 6, 11, 7, 5, 15, 9, 4]}) df.groupby('category')[['price']].diff() You'll get a new data frame back (thanks to the double square brackets around 'price'), showing the difference for each row from the previous occurrence of that category. However, if you want to know which category is which, you'll probably want to join it back to the original data frame: df.groupby('category')[['price']].diff().join(df, rsuffix='_df')
Method chaining in Pandas
18:17
Python and Pandas with Reuven Lerner
Рет қаралды 2,1 М.
Understanding "with" and Python's context managers
14:00
Python and Pandas with Reuven Lerner
Рет қаралды 998
Watermelon magic box! #shorts by Leisi Crazy
00:20
Leisi Crazy
Рет қаралды 53 МЛН
Стойкость Фёдора поразила всех!
00:58
МИНУС БАЛЛ
Рет қаралды 4,6 МЛН
Win This Dodgeball Game or DIE…
00:36
Alan Chikin Chow
Рет қаралды 38 МЛН
When should I use a "groupby" in pandas?
8:25
Data School
Рет қаралды 245 М.
How OCaml Makes Ints Speedy | Prime Reacts
20:21
ThePrimeTime
Рет қаралды 52 М.
LLM Function Calling for Emacs: Name Generation
8:47
Andrew Hyatt
Рет қаралды 309
Selecting rows in Pandas using .loc and lambda
9:04
Python and Pandas with Reuven Lerner
Рет қаралды 1,1 М.
*args and **kwargs - what are they, and how are they different?
10:42
Python and Pandas with Reuven Lerner
Рет қаралды 1,4 М.
Five mistakes companies make teaching Python to their staff
8:45
Python and Pandas with Reuven Lerner
Рет қаралды 748
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 232 М.
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
Python dataclasses will save you HOURS, also featuring attrs
8:50
Watermelon magic box! #shorts by Leisi Crazy
00:20
Leisi Crazy
Рет қаралды 53 МЛН