Pandas Example – Write a Pandas program to extract a single row, rows and a specific value from a MultiIndex levels DataFrame

(Python Example for Beginners)

 

Write a Pandas program to extract a single row, rows and a specific value from a MultiIndex levels DataFrame.

 

Sample Solution:

Python Code :


import pandas as pd 
import numpy as np

sales_arrays = [['sale1', 'sale1', 'sale2', 'sale2', 'sale3', 'sale3', 'sale4', 'sale4'],
          ['city1', 'city2', 'city1', 'city2', 'city1', 'city2', 'city1', 'city2']]

sales_tuples = list(zip(*sales_arrays))
sales_index = pd.MultiIndex.from_tuples(sales_tuples, names=['sale', 'city'])
print(sales_tuples)

print("nConstruct a Dataframe using the said MultiIndex levels: ")
df = pd.DataFrame(np.random.randn(8, 5), index=sales_index)
print(df)

print("nExtract a single row from the said dataframe:")
print(df.loc[('sale2', 'city2')])
print("nExtract a single row from the said dataframe:")
print(df.loc[('sale2', 'city2')])

print("nExtract number of rows from the said dataframe:")
print(df.loc['sale1'])
print("nExtract number of rows from the said dataframe:")
print(df.loc['sale3'])

print("nExtract a single value from the said dataframe:")
print(df.loc[('sale1', 'city2'), 1])
print("nExtract a single value from the said dataframe:")
print(df.loc[('sale4', 'city1'), 4])

Sample Output:

[('sale1', 'city1'), ('sale1', 'city2'), ('sale2', 'city1'), ('sale2', 'city2'), ('sale3', 'city1'), ('sale3', 'city2'), ('sale4', 'city1'), ('sale4', 'city2')]

Construct a Dataframe using the said MultiIndex levels: 
                    0         1         2         3         4
sale  city                                                   
sale1 city1  1.138551  0.507722 -0.870609 -0.186479 -1.038967
      city2 -0.002357  0.227624 -0.146152 -0.185473 -0.741184
sale2 city1 -1.307382  0.846347 -1.011645 -1.354593  2.208438
      city2  0.895843  0.350624  0.674705 -0.920561  0.610004
sale3 city1  0.571192  0.417562 -1.580535 -0.170085  1.258469
      city2  0.455347 -0.285652 -0.632070 -1.259128  0.710763
sale4 city1  0.178355  1.561962  1.627784 -0.097158  1.340233
      city2 -1.211935  0.256773  0.584134  1.505608 -1.559970

Extract a single row from the said dataframe:
0    0.895843
1    0.350624
2    0.674705
3   -0.920561
4    0.610004
Name: (sale2, city2), dtype: float64

Extract a single row from the said dataframe:
0    0.895843
1    0.350624
2    0.674705
3   -0.920561
4    0.610004
Name: (sale2, city2), dtype: float64

Extract number of rows from the said dataframe:
              0         1         2         3         4
city                                                   
city1  1.138551  0.507722 -0.870609 -0.186479 -1.038967
city2 -0.002357  0.227624 -0.146152 -0.185473 -0.741184

Extract number of rows from the said dataframe:
              0         1         2         3         4
city                                                   
city1  0.571192  0.417562 -1.580535 -0.170085  1.258469
city2  0.455347 -0.285652 -0.632070 -1.259128  0.710763

Extract a single value from the said dataframe:
0.22762367059081048

Extract a single value from the said dataframe:
1.340233465712309

 

Pandas Example – Write a Pandas program to extract a single row, rows and a specific value from a MultiIndex levels DataFrame

Sign up to get end-to-end “Learn By Coding” example.


Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.