🏆 Pivot | 🥇 Beats 90% | ✨ Most Efficient | ✔ Explained | Python | Pandas
Proof of 89.39%CodeDetailed Explaination

Importing the pandas library:
import pandas as pd
This line imports the pandas library, which is a powerful data manip
# Proof of 89.39%
<!-- Describe your first thoughts on how to solve this problem. -->
![image.png](https://assets.leetcode.com/users/images/4ca81b14-faed-4d20-b70e-4da5a04ce85f_1735736410.2406898.png)

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

def pivotTable(weather: pd.DataFrame) -> pd.DataFrame:
    return weather.pivot(columns='city', index='month', values='temperature')

```

# Detailed Explaination
<!-- Describe your approach to solving the problem. -->

1. **Importing the pandas library:**
   ```python
   import pandas as pd
   ```
   This line imports the `pandas` library, which is a powerful data manipulation and analysis tool in Python. It allows for various operations on structured data, such as DataFrames.

2. **Defining the function `pivotTable`:**
   ```python
   def pivotTable(weather: pd.DataFrame) -> pd.DataFrame:
   ```
   This defines a function named `pivotTable` that takes a single argument `weather`, which is expected to be a pandas DataFrame. The function returns a pandas DataFrame as well.

3. **Pivoting the DataFrame:**
   ```python
   return weather.pivot(columns='city', index='month', values='temperature')
   ```
   This line performs the main task of the function. It uses the `pivot` method from pandas to pivot the DataFrame `weather`. The `pivot` method reshapes the DataFrame by transforming or pivoting the underlying data:
   - **`columns='city'`:** This specifies that the unique values in the 'city' column will become the columns of the resulting DataFrame.
   - **`index='month'`:** This specifies that the unique values in the 'month' column will become the index (rows) of the resulting DataFrame.
   - **`values='temperature'`:** This specifies that the values in the 'temperature' column will populate the cells of the resulting DataFrame.

**Example:**
Here's an example to illustrate how this function works:

```python
import pandas as pd

# Example DataFrame
data = {
    'city': ['New York', 'Los Angeles', 'New York', 'Los Angeles'],
    'month': ['Jan', 'Jan', 'Feb', 'Feb'],
    'temperature': [30, 60, 32, 65]
}
weather = pd.DataFrame(data)

# Before pivoting
print("Original DataFrame:")
print(weather)

# After pivoting
pivoted_weather = pivotTable(weather)
print("\nPivoted DataFrame:")
print(pivoted_weather)
```

**Output:**
```
Original DataFrame:
          city month  temperature
0     New York   Jan           30
1  Los Angeles   Jan           60
2     New York   Feb           32
3  Los Angeles   Feb           65

Pivoted DataFrame:
city      Los Angeles  New York
month                           
Feb                65        32
Jan                60        30
```

In this example, the function `pivotTable` takes a DataFrame `weather` and pivots it such that the 'city' column values ('New York' and 'Los Angeles') become the columns, the 'month' column values ('Jan' and 'Feb') become the index, and the 'temperature' column values fill the corresponding cells.


# Complexity
The time complexity of the provided code primarily depends on the `pivot` operation. Here is the detailed analysis:

1. **Pivot Operation:**
   ```python
   return weather.pivot(columns='city', index='month', values='temperature')
   ```
   The `pivot` method in pandas reshapes the DataFrame based on the specified `columns`, `index`, and `values`. Internally, the `pivot` method performs the following steps:
   - It groups the data by the specified `index` ('month') and `columns` ('city').
   - It extracts the specified `values` ('temperature') for the new pivoted DataFrame.

   The main steps involve grouping and aggregating data, which generally involves:
   - Sorting or hashing the data based on the `index` and `columns`.
   - Rearranging and aggregating the values to form the pivoted DataFrame.

   The time complexity of the `pivot` operation can be considered approximately \(O(n log n)), where \(n\) is the number of rows in the DataFrame. This complexity arises from the sorting or hashing operations required to group the data efficiently.

**Summary:**
- **Time Complexity:** O(n log n), where \(n\) is the number of rows in the DataFrame.
- **Space Complexity:** O( c * m)\, where \(n\) is the number of unique values in the `index` (rows) and \(m\) is the number of unique values in the `columns` (cities), as a new DataFrame is created to store the pivoted result.

The pivot operation is generally efficient for typical DataFrame sizes, but for very large DataFrames, the time complexity can become significant due to the sorting or hashing operations required to group the data.

![image.png](https://assets.leetcode.com/users/images/a8f1f3e6-5b6e-4654-bb8b-7059e621b9ef_1735736721.7759562.png)



['Python3', 'Pandas']