Programming Essentials Python - Overview of Pandas Libraries - Projecting and Filtering

Let us understand how to project as well as filter data in Data Frames.

[Embed Video Here]

Key Concepts Explanation

Projecting Data

To project data, you can use syntax like orders.order_date or orders['order_date'] to display specific columns. For example:

orders['order_date']

To project multiple columns, you can use:

order_items[['order_item_order_id', 'order_item_subtotal']]

Filtering Data

To filter data based on specific conditions, you can use various methods. For example, to filter for items with order_item_order_id as 2, you can use:

order_items[order_items['order_item_order_id'] == 2]

You can also filter based on multiple conditions like order_item_subtotal range using:

order_items[(order_items['order_item_order_id'] == 2) & ((order_items['order_item_subtotal'] >= 150) & (order_items['order_item_subtotal'] <= 250))]

The video explains further details with examples and explanations.

Hands-On Tasks

  1. Get all the orders placed by customer_id 12431 using:
orders[orders['order_customer_id'] == 12431]
  1. Get all the orders placed by customer_id 12431 for January 2014 using:
orders[(orders['order_customer_id'] == 12431) & (orders['order_date'].str.startswith('2014-01'))]
  1. Get all orders placed by customer 12431 in January 2014 with status PENDING_PAYMENT or PROCESSING using:
orders[(orders['order_customer_id'] == 12431) & (orders['order_date'].str.startswith('2014-01')) & (orders['order_status'].isin(['PROCESSING', 'PENDING_PAYMENT']))]

Conclusion

In this article, we covered the essential topics of projecting and filtering data in Data Frames. We discussed how to project specific columns and filter data based on conditions. I encourage you to practice these tasks and engage with the community for further learning.

Watch the video tutorial here