My Coding > Programming language > Python > Python libraries and packages > Python Panda

Python Panda (Page: 8)

Go to Page:

  1. Panda Series;
  2. Pandas DataFrame: Creation;
  3. Pandas: Create test DataFrame;
  4. Pandas DataFrame: Add/Remove;
  5. Pandas DataFrame: Export/Import;
  6. Panda search and select;
  7. Pandas Cheat Sheet;
  8. Pandas: MultiIndex DataFrame;

DataFrame in Pandas can have more that one index column. In this case these DataFrames are called multi-index dataframes. In this short artile, I will show you how to create and use Multi-index DataFrame

Creation MultiIndex DataFrame

We have table with Shape, Color, Count and Size information for some objects stored in CSV data. First of all, we will read CSV this dataset into a dataframe without any indexes.

import pandas as pd
import io
data = io.StringIO('''Shape,Color,Count,Size
dfu = pd.read_csv(data)
DataFrame without index
DataFrame without index
Our test DataFrame without any indexes

At the second step, we will declare columns ‘Shape’ and ‘Color’ as index columns

df = dfu.set_index(['Shape', 'Color'])
DataFrame with two indexes.
DataFrame with two indexes.
DataFrame with two columns ‘Shape’ and ‘Color’, declared as an indexes

Grouping by indexes

Group DataFrame by one index

It is possible to group by any indexes and do some calculations within these groups. For example, lets group all objects by its ‘Shape’ and calculate mean values for the rest of parameters

Mean values for ‘<b>Shape</b>’
Mean values for ‘Shape
Calculation of mean values for all groups in ‘Shape

It is possible to group by any indexes. For example let’s count how many data we have for different ‘Color’.

It is important to remember, that count() give number of elements without NaN and size() give total number of elements, with NaN

Count group by ‘<b>Color</b>’
Count group by ‘Color
Grouped by ‘Color’ and then counted. Obviously that all columns will have the same values, because there are the same amount of information in every column.

Group DataFrame by two indexes

It is possible to group by few indexes. And also it is possible to specify an order of grouping. So let’s group by ‘Color’ and then by ‘Shape’ and then let’s calculate the average values in every group.

Mean of group by ‘<b>Color</b>’ and ‘<b>Shape</b>’
Mean of group by ‘Color’ and ‘Shape
Grouped by ‘Color’ and then by ‘Shape’ and calculating mean value in every group

Use group

After grouping, it it is possible to call one group

gb = df.groupby(level=['Color','Shape'])
gb.get_group(('Green', 'Oval'))
Using one group
Using one group
After group by it is possible to use one group by specify it’s indexes

Go to Page: 1; 2; 3; 4; 5; 6; 7; 8;

Published: 2021-11-05 09:11:16
Updated: 2021-12-17 02:48:39

Last 10 artitles

9 popular artitles

© 2020 -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.