Python Pandas Shift Tutorial (With Interview Question)

Опубликовано: 04 Июль 2023
на канале: Ryan & Matt Data Science
2,607
74

🧠 Don’t miss out! Get FREE access to my Skool community — packed with resources, tools, and support to help you with Data, Machine Learning, and AI Automations! 📈 https://www.skool.com/data-and-ai-aut...

Today we are taking a look at how you can use the shift function within a Python Pandas dataframe! This occasionally is asked on more complicated interview questions and is a nice add to use at work.

Code: https://ryanandmattdatascience.com/pa...

🚀 Hire me for Data Work: https://ryanandmattdatascience.com/da...
👨‍💻 Mentorships: https://ryanandmattdatascience.com/me...
📧 Email: ryannolandata@gmail.com
🌐 Website & Blog: https://ryanandmattdatascience.com/
🖥️ Discord:   / discord  
📚 *Practice SQL & Python Interview Questions: https://stratascratch.com/?via=ryan
📖 *SQL and Python Courses: https://datacamp.pxf.io/XYD7Qg

🍿 WATCH NEXT
Python Pandas Playlist:    • Python Pandas for Beginners  
Python Split Columns:    • How to Split up Columns in Python Pandas  
Python Groupby:    • The Complete Guide to Python Pandas Groupby  
Python Lambda Functions:    • Python Pandas Lambda Function Tutorial Wit...  


In this video, I walk you through the Python pandas shift function, a powerful tool that lets you transform your DataFrame to compare different rows or columns. The shift function is essential for time-series analysis, calculating differences between consecutive records, and solving common data analysis problems like comparing stock prices across different days or tracking transaction changes over time.

We start with simple examples showing how to shift data up, down, left, and right using positive and negative values. I demonstrate how to handle those annoying NaN values that appear after shifting by using the fill_value parameter. Then we explore the axis parameter to control whether you're shifting rows or columns, with clear visual examples of how each transformation works.

The second half of the video tackles a real coding interview question where we analyze transaction data. You'll learn how to combine shift with groupby to compare a person's current transaction amount with their previous one, calculate differences, and filter results to find specific patterns. This practical example shows exactly how shift functions are used in real data analysis scenarios, from e-commerce platforms to payment processors.

By the end of this tutorial, you'll confidently use the pandas shift function in your own projects and ace any interview questions involving row-to-row comparisons or time-based calculations.

TIMESTAMPS
00:00 Introduction to Pandas Shift Function
00:41 Setup & Basic Data Frame
01:29 Basic Shift Down Example
02:01 Shifting Multiple Rows
02:35 Shifting Backwards (Negative Shift)
03:01 Replacing NaN Values with Fill Value
04:05 Shifting Left and Right (Axis Parameter)
05:44 Coding Interview Question Setup
07:24 Sorting Data by Person ID
08:36 Creating Previous Amount Column with Group By
10:40 Creating Next Amount Column
11:27 Solving the Interview Question with Query
12:27 Calculating Value Difference Between Transactions

OTHER SOCIALS:
Ryan’s LinkedIn:   / ryan-p-nolan  
Matt’s LinkedIn:   / matt-payne-ceo  
Twitter/X: https://x.com/RyanMattDS

Who is Ryan
Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Who is Matt
Matt is the founder of Width.ai, an AI and Machine Learning agency. Before starting his own company, he was a Machine Learning Engineer at Capital One.

*This is an affiliate program. We receive a small portion of the final sale at no extra cost to you.


На этой странице сайта вы можете посмотреть видео онлайн Python Pandas Shift Tutorial (With Interview Question) длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Ryan & Matt Data Science 04 Июль 2023, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 2,607 раз и оно понравилось 74 зрителям. Приятного просмотра!