2889. Reshaping data: Pivot | Explanation pivot() and pivot_table() with examples
Intuition\n Describe your first thoughts on how to solve this problem. \nPandas provides DataFrame.pivot() and DataFrame.pivot_table() methods for reshaping tab
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nPandas provides `DataFrame.pivot()` and `DataFrame.pivot_table()` methods for reshaping tables. In this case, we can use both, but there are a few nuances to pay attention to:\n\n- `pivot()` doesn\'t allow for any aggregations of the data, but `pivot_table()` does and calculates `mean` by default. Before using `pivot_table()`, it is good to check the duplicates to avoid unexpected results such as huge numbers when using the `sum` function.   \n- `pivot()` can only handle unique rows specified by index and columns and will raise a `ValueError` in case the table contains duplicates, while `pivot_table()` will work well.\n- `pivot()` works with various data types, whereas `pivot_table()` requires numeric data for aggregation.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIn this case, it is possible to use `pivot()` because we only want to reshape the table based on the index without aggregating data *(as expected by the description too:))*.  \n\nUsing `pivot_table()\' I would choose `mean\' (`max\' or `min\') for the aggregation to deal with duplicates.\n\n# Code\n`DataFrame.pivot()`:\n```python\nimport pandas as pd\n\ndef pivotTable(weather: pd.DataFrame) -> pd.DataFrame:\n    w = weather.pivot(\n        index=\'month\', \n        columns=\'city\', \n        values=\'temperature\'\n    )\n    return w\n```\n\n`DataFrame.pivot_table()`:\n```python\nimport pandas as pd\n\ndef pivotTable(weather: pd.DataFrame) -> pd.DataFrame:\n    w = weather.pivot_table(\n        index=\'month\', \n        columns=\'city\', \n        values=\'temperature\', \n        aggfunc=\'mean\'\n    )\n    return w\n```\n\n---\n# Behavior of methods in examples\nCopy and run: \n```python\nimport pandas as pd\n\ndf1 = pd.DataFrame({\n    \'city\': [\n        \'Jacksonville\', \'Jacksonville\', \'Jacksonville\', \'Jacksonville\',\'Jacksonville\',\n        \'ElPaso\', \'ElPaso\', \'ElPaso\', \'ElPaso\', \'ElPaso\'\n        ],\n    \'month\': [\n        \'January\', \'February\', \'March\', \'April\', \'May\', \n        \'January\', \'February\', \'March\', \'April\', \'May\'\n        ],\n    \'temperature\': [13, 23, 38, 5, 34, 20, 6, 26, 2, 43]\n})\n\ndf2 = pd.DataFrame({\n    \'city\': [\n        \'Jacksonville\', \'Jacksonville\', \'Jacksonville\', \'Jacksonville\',\'Jacksonville\', \n        \'Jacksonville\',\n        \'ElPaso\', \'ElPaso\', \'ElPaso\', \'ElPaso\', \'ElPaso\'\n        ],\n    \'month\': [\n        \'January\', \'February\', \'March\', \'April\', \'May\', \n        \'January\',\n        \'January\', \'February\', \'March\', \'April\', \'May\'\n        ],\n    \'temperature\': [13, 23, 38, 5, 34, 230, 20, 6, 26, 2, 43]\n})\n\n# 1. Cases for pandas.DataFrame.pivot:\n# Case 1.1: no duplicates in table\ndf1_pivot = df1.pivot(index=\'month\', columns=\'city\', values=\'temperature\')\nprint(\'Use pandas.DataFrame.pivot with table without duplicates:\')\nprint(df1_pivot)\n# Case 1.2: with duplicates in table\nprint(\'Using pandas.DataFrame.pivot with duplicates contained in the table:\')\ntry:\n    df2_pivot = df2.pivot(index=\'month\', columns=\'city\', values=\'temperature\')\nexcept ValueError:\n    print(\'ValueError: Index contains duplicate entries, cannot reshape\')\nelse:\n    print(df2_pivot)\n\n# 2. Cases for pandas.DataFrame.pivot_table:\n# Case 2.1: no duplicates in table\ndf1_pivot_table = df1.pivot_table(\n        values=\'temperature\', \n        index=\'month\', \n        columns=\'city\', \n        aggfunc=\'sum\'\n    )\nprint(\'Use pandas.DataFrame.pivot_table with table without duplicates:\')\nprint(df1_pivot_table)\n# Case 2.2: with duplicates in table\ndf2_pivot_table = df2.pivot_table(\n        values=\'temperature\', \n        index=\'month\', \n        columns=\'city\', \n        aggfunc=\'sum\'\n    )\nprint(\'Use pandas.DataFrame.pivot_table with duplicates contained in the table:\')\nprint(df2_pivot_table)\nprint(\'OMG! I never go to Jacksonville in January! 243 is too much! :)\')\n```\n\nThanks for reading! \nHave a great day with python ;)
['Pandas']