|
Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Yesterday we introduced NumPy and created few arrays. Today we learn how to access them. Think about it - You have a dataset with 10,000 prices. You need the first 100. Or the last one. Or every other value. That's indexing and slicing. Without it, arrays are useless. With it, you control your data. Let's get to the snippets. 🚩 Get value by position (index) [0] means 1st position and so on. This array can be daily sales value.
In:
import numpy as np prices = np.array([100, 150, 200, 250, 300, 400]) print(prices[0]) print(prices[2])
Out:
100 200 🔙 Negative Indexing - Start from back Get most recent or second to last sales
In:
print(prices[-1]) print(prices[-2])
Out:
400 300 🍰 Slice a range - first or last Example: First 3 months sales. Note: 3 is not included (only 0,1,2)
In:
print(prices[:3]) print(prices[2:])
Out:
[100 150 200] [200 250 300 400] 🍕 Slice again - from between includes 1,2,3 index positions
In:
print(prices[1:4])
Out:
[150 200 250]
🚫 Slice by skipping sales every alternate day. Start from 0 and go to 2,4..index positions
In:
print(prices[::2])
Out:
[100 200 300]
⏭ Skip with a different start Start from 1 and go to 3,5..index positions
In:
print(prices[1::2])
Out:
[150 250 400]
✍ Modify value using index
In:
print(prices) prices[5] = 29 print(prices)
Out:
[100 150 200 250 300 400] [100 150 200 250 300 29] Quick Excel Shortcut 💡Ctrl + D Fill all cells below with the content or formula from the one above. Handy when copying formulas or values down a column without dragging ⭐📣 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",...