Found 10476 Articles for Python

Python Pandas - Rearrange levels using level name in MultiIndex

AmitDiwan
Updated on 19-Oct-2021 08:05:45

331 Views

To rearrange levels using level name in MultiIndex, use the MultiIndex.reorder_levels() method in Pandas. Pass the levels (level names) to be rearranged as arguments.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob'], [50, 30, 40, 70]] The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiIndex −multiIndex = pd.MultiIndex.from_arrays(arrays, names=('rank', 'student', 'points'))Reorder levels of MultiIndex. The "order" parameter is used to set the level name in a ... Read More

Python Pandas - Swap levels of a MultiIndex

AmitDiwan
Updated on 19-Oct-2021 08:02:30

1K+ Views

To swap levels of a MultiIndex, use the swaplevel() method in Pandas. The levels to be swapped should be mentioned as arguments.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob'], [50, 30, 40, 70]] The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiIndex −multiIndex = pd.MultiIndex.from_arrays(arrays, names=('rank', 'student', 'points'))Swap levels of MultiIndex using swaplevel(). The 1st parameter is the first level of index to be swapped. ... Read More

Python Pandas - Return MultiIndex with multiple levels removed using the level names

AmitDiwan
Updated on 19-Oct-2021 08:00:02

168 Views

To return MultiIndex with multiple levels removed using the level names, use the MultiIndex.droplevel() method and set the multiple levels (level name) to be removed as arguments.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob'], [50, 30, 40, 70]] The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiIndex −multiIndex = pd.MultiIndex.from_arrays(arrays, names=('rank', 'student', 'points'))Drop a specific level from MultiIndex. The levels to be dropped is set ... Read More

Python Pandas - Return MultiIndex with requested level removed using the level name

AmitDiwan
Updated on 19-Oct-2021 07:57:35

158 Views

To return MultiIndex with requested level removed using the level name, use the MultiIndex.droplevel() method and set the level (level name) to be removed as an argument.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob']] The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiIndex −multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))Drop a specific level from MultiIndex. The level to be dropped is set as the level name in ... Read More

Python Pandas - Return MultiIndex with requested level removed

AmitDiwan
Updated on 19-Oct-2021 07:54:26

136 Views

To return MultiIndex with requested level removed, use the MultiIndex.droplevel() method in Pandas. Set the level to be removed as an argument.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob']] The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiIndex −multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))Drop a specific level. The level is 1 i.e. level 1 gets dropped” −print("Multi-index after dropping a level...", multiIndex.droplevel(1)) ExampleFollowing is the code ... Read More

Python Pandas - How to Sort MultiIndex at a specific level in descending order

AmitDiwan
Updated on 19-Oct-2021 07:50:58

2K+ Views

To create a MultiIndex, use the from_arrays() method. However, to sort MultiIndex at a specific level, use the multiIndex.sortlevel() method in Pandas. Set the level as an argument. To sort in descending order, use the ascending parameter and set to False.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob']] The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiIndex −multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))Sort MultiIndex. The specific ... Read More

Python Pandas - How to Sort MultiIndex at a specific level

AmitDiwan
Updated on 19-Oct-2021 07:47:58

320 Views

To create a MultiIndex, use the from_arrays() method. However, to sort MultiIndex at a specific level, use the multiIndex.sortlevel() method in Pandas. Set the level as an argument.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays:arrays = [[2, 4, 3, 1], ['Peter', 'Chris', 'Andy', 'Jacob']] The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiIndex −multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))Sort MultiIndex. The specific level to sort is set as a parameter i.e. level 1 here −print("Sort MultiIndex ... Read More

Python Pandas - How to Sort MultiIndex

AmitDiwan
Updated on 19-Oct-2021 07:43:44

886 Views

To create a MultiIndex, use the from_arrays() method. However, to sort MultiIndex, use the multiIndex.sortlevel(). method in Pandas.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −arrays = [[2, 4, 3, 1], ['John', 'Tim', 'Jacob', 'Chris']] The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiInde −multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))Sort MultiIndex. The default sorts at level 0 −print("Sort MultiIndex...", multiIndex.sortlevel()) ExampleFollowing is the code −import pandas as pd # MultiIndex is a multi-level, or hierarchical, ... Read More

Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns and substitute index level names

AmitDiwan
Updated on 19-Oct-2021 07:40:30

384 Views

To create a DataFrame with the levels of the MultiIndex as columns, use the MultiIndex.to_frame() method. Substitute index level names using the name parameter.At first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −arrays = [[1, 2, 3, 4], ['John', 'Tim', 'Jacob', 'Chris']]The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiIndex −multiIndex = pd.MultiIndex.from_arrays(arrays)Create a DataFrame with the levels of the MultiIndex as columns using to_frame(). Use the "name" parameter and pass the names to substitute index ... Read More

Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns but avoid setting the index of the returned DataFrame

AmitDiwan
Updated on 19-Oct-2021 07:34:01

177 Views

To create a DataFrame with the levels of the MultiIndex as columns, use the multiIndex.to_frame(). The index parameter is set False to avoid setting the index of the returned DataFrameAt first, import the required libraries −import pandas as pdMultiIndex is a multi-level, or hierarchical, index object for pandas objects. Create arrays −arrays = [[1, 2, 3, 4], ['John', 'Tim', 'Jacob', 'Chris']] The "names" parameter sets the names for each of the index levels. The from_arrays() is used to create a MultiIndex −multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))Create a DataFrame with the levels of the MultiIndex as columns using to_frame(). Use the ... Read More

Advertisements