Solving LeetCode - 1164. Product Price at a Given Date IN SQl - WITH cte AS (SELECT * FROM Products WHERE change_date <= '2019-08-16'), cte1 AS (SELECT *, RANK() OVER (PARTITION BY product_id ORDER BY change_date DESC) AS rnk FROM cte) SELECT product_id, new_price AS price FROM cte1 WHERE rnk = 1 UNION SELECT product_id, 10 AS price FROM Products WHERE product_id NOT IN (SELECT product_id FROM cte1); IN PANDAS- import pandas as pd def price_at_given_date(products: pd.DataFrame) -> pd.DataFrame: filtered = products[products['change_date'] <= '2019-08-16'] filtered = filtered.sort_values(['product_id', 'change_date'], ascending=[True, False]) latest = filtered.drop_duplicates(subset=['product_id'], keep='first') all_products = products['product_id'].unique() latest_ids = latest['product_id'].unique() missing_ids = set(all_products) - set(latest_ids) missing_df = pd.DataFrame({'product_id': list(missing_ids), 'price': 10}) latest = latest[['product_id', 'new_price']].rename(columns={'new_price': 'price'}) result = pd.concat([latest, missing_df], ignore_index=True) return result.sort_values('product_id').reset_index(drop=True) #LeetCode #Python #Pandas #SQL #DataEngineering #CodingPractice #ProblemSolving #DataAnalysis #SQLtoPandas #googleanalytics
Solved LeetCode 1164 with SQL and Pandas
More Relevant Posts
-
Solving LeetCode - 1045. Customers Who Bought All Products IN SQl - select customer_id from Customer group by customer_id having count(distinct product_key)=(select count(distinct(product_key)) from Product) IN PANDAS- import pandas as pd def customers_who_bought_all_products(customer: pd.DataFrame, product: pd.DataFrame) -> pd.DataFrame: total_products = product['product_key'].nunique() customer_counts = (customer.groupby('customer_id')['product_key'].nunique().reset_index()) result = customer_counts[customer_counts['product_key'] == total_products][['customer_id']] return result #LeetCode #Python #Pandas #SQL #DataEngineering #CodingPractice #ProblemSolving #DataAnalysis #SQLtoPandas #googleanalytics
To view or add a comment, sign in
-
Most people save pandas DataFrames as .csv or .xlsx. But here’s the truth — those formats are dumb storage. They don’t remember types, indexes, or even NaNs properly. That’s where Pickle (.pkl) comes in. Pickle isn’t just a file — it’s a snapshot of your Python object. When you load it back, you get the exact same DataFrame, including: • Column dtypes • Index • Categoricals • Complex objects Now, wrap that .pkl in gzip compression and you get .pkl.gz: df.to_pickle("data.pkl.gz", compression="gzip") ✅ Exact data recovery (no conversions) ✅ 70–90% smaller than .pkl ✅ 10–15x faster load than .csv ✅ Perfect for caching models, merged data, or preprocessed outputs In short — Pickle saves structure. Gzip saves space. Together, .pkl.gz saves your time.
To view or add a comment, sign in
-
🚀 𝐒𝐐𝐋 𝐭𝐨 𝐏𝐲𝐭𝐡𝐨𝐧 — 𝐓𝐡𝐞 𝐐𝐮𝐢𝐜𝐤𝐬𝐭𝐚𝐫𝐭 𝐆𝐮𝐢𝐝𝐞 𝐘𝐨𝐮 𝐍𝐞𝐞𝐝! If you’re transitioning from SQL to Python (Pandas) for data analysis, this visual guide is your cheat sheet! 💡 SQL Certification Course :- https://lnkd.in/dVRSnppf Here’s how common SQL operations translate directly into Python code: 1️⃣ Filtering: WHERE ➜ df[df['column'] == 'value'] 2️⃣ Ordering: ORDER BY ➜ df.sort_values(by='column') 3️⃣ Removing Duplicates: DISTINCT ➜ df.drop_duplicates() 4️⃣ Handling Missing Data: COALESCE() ➜ df['column'].fillna() 5️⃣ Changing Data Types: CAST() ➜ df['column'].astype() 6️⃣ Renaming Columns: AS new_col ➜ df.rename() 7️⃣ Aggregations: SUM(), AVG(), MIN(), MAX() ➜ .sum(), .mean(), .min(), .max() 8️⃣ Counting: COUNT() ➜ .count() 9️⃣ Percentiles: PERCENTILE_CONT() ➜ .quantile() 🔟 Joins & Unions: JOIN, UNION ALL ➜ pd.merge(), pd.concat()
To view or add a comment, sign in
-
-
Leetcode 75 + SQL 50 Day 4 (🟡 - Medium): 151. Reverse words in a string Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. Initial Thoughts: - This should probably be an easy question if not for the extra spaces - first thought put into a list then check for the spaces, but this doesn't work because when put into a list it'll make each character one element - I knew there was something that you could make each word in a sentence as one element, but it slipped my mind so I looked it up (python split() function), or I guess cheated a little bit Solution: - use .split() to split the sentence into a list with the default separator being any white spaces (removes any kind of space) - use the two pointer method (left = 0, right = length of list minus one) to go through the list maximum one time - going through the list, swapping each element in the list, but I set a while loop so that when the left and right pointers cross over it would finish and return the list joined by a space value
To view or add a comment, sign in
-
-
Your first PowerBI dashboard will be awful. Your first SQL script won't be production standard. The first time you write Python code it will take a long time. Stop thinking you'll be an expert overnight. It takes time to develop any skill. Set small goals and be proud when you tick them off. Lot's of small wins > no wins at all --- If you want more tips like this, my newsletter (The Data Dose) is right up your street. One email, every Friday, aimed at helping you get better with data. Link in the first comment 👇🏻
To view or add a comment, sign in
-
🐼 Pandas Essential Commands Cheatsheet — Learn the Most Used Functions Fast Whether you’re cleaning data or doing analysis, these commands are your daily essentials in Python Pandas 👇 📥 Load & Inspect Data → pd.read_csv('file.csv') → Load data from a CSV file → df.head() → Display first 5 rows → df.shape → Check dimensions (rows, columns) → df.info() → View datatypes and memory info → df.describe() → Generate summary statistics 📊 Select & Filter Data → df['column'] → Select one column → df[['col1','col2']] → Select multiple columns → df.loc[row_label] → Access rows by label → df.iloc[row_index] → Access rows by index position → df.query('column > value') → Filter using conditions 🧹 Handle Missing Data → df.dropna() → Remove missing values → df.fillna(value) → Fill missing values 📈 Sort, Group & Aggregate → df.sort_values('column') → Sort data → df.groupby('column').agg() → Group and summarize data → df.value_counts() → Count unique values 🔗 Combine & Modify Data → df.merge(df2, on='key') → Merge dataframes → df.rename(columns={'old':'new'}) → Rename columns → df.drop('column', axis=1) → Remove column → df.reset_index() → Reset index 🎓 Learn Pandas in Action (Free): 🔗 https://lnkd.in/dc2p2j_W 🔗 https://lnkd.in/d5iyumu4 ✍️ Credit: Gina Acosta #Python #Pandas #DataAnalysis #MachineLearning #DataScience #ProgrammingValley #Analytics
To view or add a comment, sign in
-
-
Hey Folks , 🧩 Ever seen __init__.py and thought — “Why do we even need this empty file?” 🤔 As Data Engineers, we work with Airflow, Dataflow, and BigQuery — and our pipelines usually have dozens of Python scripts. That’s where __init__.py quietly steps in and saves the day. Let’s understand it in simple terms 👇 🚫 Without __init__.py You might have a folder structure like: Copy code project/ ├── dags/ │ ├── etl/ │ │ ├── extract.py │ │ └── transform.py │ └── main_dag.py Now, if you try to import: Copy code Python from dags.etl.extract import extract_data Python says ❌ ModuleNotFoundError Because it sees dags and etl as normal folders, not Python packages. ✅ Add __init__.py and magic happens Once you add: Copy code project/ ├── dags/ │ ├── __init__.py │ ├── etl/ │ │ ├── __init__.py │ │ ├── extract.py │ │ └── transform.py │ └── main_dag.py Python now knows: “This folder is a package. You can safely import from it.” Your imports start working smoothly: Copy code Python from dags.etl.extract import extract_data ⚙️ Bonus — You can add setup logic You can even write code inside __init__.py, like: Copy code Python print("ETL package loaded successfully!") This runs automatically when your package is imported — useful for initializing configs, logging, or environment setup. 💡 Pro Tip Even though Python 3.3+ allows “namespace packages” without __init__.py, in real-world data projects (Airflow, Dataflow, PySpark, etc.), adding it is still best practice because: It avoids confusing import errors Keeps your project structure clean Ensures compatibility across environments So yes, it may look blank — but that tiny __init__.py is the glue that holds your ETL structure together 💪 #DataEngineering #Python #Airflow #ETL #BigQuery #Dataflow #CodingLife #CleanCode #SoftwareEngineering #GCP
To view or add a comment, sign in
-
🧑💻 #MyDataJourney – A quick SQL sanity check for the experts 😅 I came across something interesting while working with window functions: SUM(SUM(column_name)) OVER () It runs perfectly — but it got me thinking 🤔 If this expression is placed inside a CTE, does it necessarily imply that there’s an outer aggregation (like a GROUP BY) happening at the higher level? Or can the window function handle that entirely on its own? I already have my own assumption, but I’d really like to hear how experienced SQL professionals interpret this behavior 👇 My brain says “yes,” my SQL says “maybe not.” 😂 #SQL #DataAnalytics #DataAnalyst #CTE #WindowFunctions #LearningInProgress #CareerInData #AskForHelp #AnalyticsCommunity #BusinessIntelligence #PowerBI #Python #Tableau #TechCommunity #DataScience #SQLQuery #AnalyticsLife
To view or add a comment, sign in
-
LeetCode 104 – Maximum Depth of a Binary Tree 🌳 Today I solved one of the classic problems in data structures: “Given the root of a binary tree, return its maximum depth.” 👉 What’s a binary tree? A binary tree is a hierarchical structure where each node has at most two children: left and right. The maximum depth is simply the longest path from the root down to the farthest leaf. 🧩 Why Recursion Works Best Trees are naturally recursive: Each node is itself the root of a smaller subtree. To solve the whole tree, you solve the left subtree and the right subtree, then combine results. This makes recursion elegant and intuitive compared to iterative approaches. ✅ My Solution public int maxDepth(TreeNode root) { if (root == null) return 0; int leftDepth = maxDepth(root.left); int rightDepth = maxDepth(root.right); return 1 + Math.max(leftDepth, rightDepth); } 📊 Complexity Time: O(n) → we visit every node once. Space: O(h) → recursion stack depends on tree height. Worst case (skewed tree): O(n) Best case (balanced tree): O(log n) 🎯 Key Takeaway Binary trees are a perfect example of why recursion is powerful. Once you “think recursively,” problems like maximum depth become straightforward. This problem reminded me that elegance in code often comes from matching the solution to the structure of the data. #LeetCode #DataStructures #BinaryTree #Recursion #Java #CodingJourney
To view or add a comment, sign in
-
-
📊 Pandas Merge vs Merge_Ordered — What’s the Difference? If you’ve worked with pandas, you’ve probably used merge() — but have you explored merge_ordered()? 🤔 Here’s a quick breakdown 👇 🔹 merge() Used for combining any two DataFrames based on common columns or indexes. ➡️ Works just like SQL joins (inner, left, right, outer) ➡️ Does not care about order — it just matches keys. pd.merge(df1, df2, on='id', how='inner') 🔹 merge_ordered() Used when order matters — ideal for time-series or sequential data. ➡️ Performs an ordered merge (keeps data sorted). ➡️ Has fill_method to handle missing values (like forward fill). pd.merge_ordered(df1, df2, on='date', fill_method='ffill') 💡 In short: Use merge() → when combining data by key (like SQL joins). Use merge_ordered() → when merging chronological or ordered data while preserving sequence. #DataScience #Python #Pandas #DataAnalytics #LearningEveryday
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development