Reshape Products Table Using `pd.melt()`
IntuitionThe given products table has store names as column headers, which is inefficient for querying and analysis. The goal is to transform this "wide-format"
### **Intuition**  
The given `products` table has store names as column headers, which is inefficient for querying and analysis. The goal is to transform this "wide-format" data into a "long-format" where each row represents a product, a store, and its corresponding price.  

This format is better for filtering, aggregating, and performing SQL-like queries.  



### **Approach**  
1. **Use `pd.melt()` to reshape the DataFrame**  
   - `id_vars='product_id'`: Keeps `product_id` as a fixed column.  
   - `var_name='store'`: Converts column names (store names) into a new column named `store`.  
   - `value_name='price'`: Moves corresponding values into a column named `price`.  

2. **Drop missing values (`NaN`)**  
   - Some products may not be available in all stores, leading to `NaN` values.  
   - `.dropna()` removes these rows to keep only valid (non-null) prices.  



### **Example**  

#### **Input (`products` DataFrame):**  
| product_id | store_A | store_B | store_C |
|------------|---------|---------|---------|
| 1          | 10      | 20      | NaN     |
| 2          | 15      | NaN     | 30      |


#### **Output (`rearrange_products_table(products)`):**  
| product_id | store  | price |
|------------|--------|-------|
| 1          | store_A | 10    |
| 1          | store_B | 20    |
| 2          | store_A | 15    |
| 2          | store_C | 30    |



### **Complexity Analysis**  
- **Time Complexity:**  **O(m . n)**
where **( m )** is the number of rows and **( n )** is the number of store columns.  
- **Space Complexity:**  **O(1)** 
as `pd.melt()` modifies the structure without extra storage (besides output).  

# Code
```pythondata []
import pandas as pd

def rearrange_products_table(products: pd.DataFrame) -> pd.DataFrame:
    return pd.melt(
        products, id_vars='product_id', var_name='store', value_name='price'
    ).dropna()
    
```


![upvote.jpeg](https://assets.leetcode.com/users/images/1dc18f3b-9e6e-4704-b68e-c2f4d823f8ad_1741371771.1206584.jpeg)



['Pandas']