|
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. 📢 Quick Announcement: At the end I will be sharing a super useful excel tip daily. So, don't miss! Yesterday we saw the lazy processing of itertools and how it handles data to save memory. That was the warm up. There are more tricks up its sleeve. Need product bundles for your store? Or generate size-color combination for your inventory? Most will write endless loops. itertools does this in one line. 🛍️ combinations to build bundles (order does not matter)
In:
items = ["A", "B", "C"] pairs = list(combinations(items, 2)) print(pairs)
Out:
[('A', 'B'), ('A', 'C'), ('B', 'C')]
🚚 permutations when order matters
In:
steps = ["Pick", "Pack", "Ship"] flows = list(permutations(steps, 2)) print(flows)
Out:
[('Pick', 'Pack'), ('Pick', 'Ship'), ('Pack', 'Pick'), ('Pack', 'Ship'), ('Ship', 'Pick'), ('Ship', 'Pack')]
🏷️ product for all combinations between 2 things
In:
sizes = ["S", "M", "L"] colors = ["Black", "White"] grid = list(product(sizes, colors)) print(grid)
Out:
[('S', 'Black'), ('S', 'White'), ('M', 'Black'), ('M', 'White'), ('L', 'Black'), ('L', 'White')]
🔗 islice to get first N without loading all
In:
stream = (n for n in range(1000)) head = list(islice(stream, 5)) print(head)
Out:
[0, 1, 2, 3, 4]
🐇 islice (start, end, step) This gives Every 3rd record
In:
data = list(range(30)) sample = list(islice(data, 5, 20, 3)) print(sample)
Out:
[5, 8, 11, 14, 17]
⏳ compress to filter basis flag
In:
items = ["A", "B", "C", "D", "E"] flags = [1, 0, 1, 0, 1] # keep where flag is 1 active = list(compress(items, flags)) print(active)
Out:
['A', 'C', 'E']
👨👦👦 groupby across similar objects
In:
from itertools import groupby sales = [("StoreA", 200), ("StoreA", 150), ("StoreB", 300), ("StoreB", 100)] for store, group in groupby(sales, key=lambda x: x[0]): print(store, list(group))
Out:
StoreA [('StoreA', 200), ('StoreA', 150)] StoreB [('StoreB', 300), ('StoreB', 100)] You can also do aggregation like count or sum store wise sales. Reply to know how! 💡 Quick Excel Tip
⭐📣 That's it for today! If you liked it, please share it with anyone who will find it useful and share your feedback below 🐼
|
Beginner to Expert in Python in just 5 minutes
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. You have a spreadsheet with 500 numbers. Your boss asks: "What do you see?" Most will freeze, scroll, guess. Not you! 3 Python functions to turn raw numbers into answers. No calculator, excel sheets. Just code. Statistics isn't boring math. It's how you make decisions with data. Using just the statistics module we find "center" of any dataset. Any data analyst must know the basics‼️ 🎯 Compute simple...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Last week we introduced numpy. Created bunch of arrays, sliced them and did some math with them. To prepare data for machine learning or any analysis, we need to learn to control their shape. Today - Reshape flat lists into grids. Flatten 2D tables back to 1D arrays. Create arrays filled with zeros, ones, or random numbers. ⏪ Recap: See dimensions first In: import numpy as np sales = np.array([120, 340,...
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. TGIF! Everyone is talking about AI. Most people take courses. You're going to build it instead. Let's create a simple sentiment analyzer that reads customer reviews and classifies them as positive or negative. No machine learning libraries—just Python. 🎯 Define list of positive and negative words In: positive_words = ["good", "great", "excellent", "amazing", "love", "best"] negative_words = ["bad",...