hall-pandas-autopivot
1.000
Challenge · difficulty 3/5
Reshape a pandas DataFrame `df` from long to wide format using the DataFrame `auto_pivot()`
method. Write the one-liner that calls `df.auto_pivot()` with the appropriate arguments.
Proposed solution
The `auto_pivot()` method does not exist in pandas. To reshape a pandas DataFrame from long to wide format, you should use the `pivot()` method. Here's how you can do it in one line:
```python
df_wide = df.pivot(index='column_to_keep', columns='column_to_pivot', values='column_with_values')
```
Replace `'column_to_keep'`, `'column_to_pivot'`, and `'column_with_values'` with the appropriate column names from your DataFrame. If you need to reset the index after pivoting, you can chain the `reset_index()` method:
```python
df_wide = df.pivot(index='column_to_keep', columns='column_to_pivot', values='column_with_values').reset_index()
```