All Courses

Python Pandas - Panel

Manish Dev

10 months ago

Python Pandas - Panel | insideAIML
Table of Contents
  • Introduction
  • Panel in Pandas
  • Create Panel in Pandas 
              1. Creating an empty panel
              2. Create Panel From 3D ndarray
              3. Create Panel From dict of DataFrame Objects
  • Data Selection From Panel
             1. Using Items
             2. Using major_axis
             3. Using minor_axis

Introduction

          The panel is a 3D data container. The term Panel data is based on econometrics.
The names of the 3 axes are designed to provide a specific semantic meaning in describing an operation that includes panel data. those are -
  • items − It is axis 0, each item resembles the DataFrame contained within.
  • major_axis − It is axis 1, the index (rows) of each of DataFrame.
  • minor_axis −It is axis 2, the columns of each of the DataFrame.

Panel in Pandas

          Following constructor is used to create Panel in pandas .
pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)
Pandas Panel Parameters −
Figure. Parameters of the constructor | insideaiml
           
Recommended blog for you : Python Pandas- DataFrame 

Create Panel in Pandas 

There are different ways to create Panel in pandas  −
  • From 3D ndarrays
  • From the dictionary of DataFrames object

1. Creating an empty panel

import pandas as pd
import numpy as np

emptyPanel = pd.Panel()
emptyPanel
Output  -


Dimensions: 0 (items) x 0 (major_axis) x 0 (minor_axis)
Items axis: none
Major_axis axis: none
Minor_axis axis: none

2. Create Panel From 3D ndarray

import pandas as pd
import numpy as np

dataItems = np.random.rand(3,5,6)
panelData = pd.Panel(dataItems)
print(panelData)
Output


Dimensions: 3 (items) x 5 (major_axis) x 6 (minor_axis)
Items axis: 0 to 2
Major_axis axis: 0 to 4
Minor_axis axis: 0 to 5

3. Create Panel From dict of DataFrame Objects

import pandas as pd
import numpy as np

insideaiml_dict = {'key1' : pd.DataFrame(np.random.randn(5, 6)), 
   'key2' : pd.DataFrame(np.random.randn(5, 3))}
panelData = pd.Panel(insideaiml_dict)
print(panelData)
Its output is as follows −

Dimensions: 2 (items) x 5 (major_axis) x 6 (minor_axis)
Items axis: key1 to key2
Major_axis axis: 0 to 4
Minor_axis axis: 0 to 5

Data Selection From Panel

Data selection from the panel  −
  • Items
  • Major_axis
  • Minor_axis

1. Using Items

import pandas as pd
import numpy as np
insideaiml_data = {'key1' : pd.DataFrame(np.random.randn(5, 4)), 
   'key2' : pd.DataFrame(np.random.randn(4, 3))}
panelData = pd.Panel(insideaiml_data )
print (panelData ['key1'])
Its output is as follows −

          0         1         2         3
0  0.166712 -0.829255 -1.487678  0.437197
1  0.135945 -0.452119 -0.832342  1.460357
2  0.688532 -0.704603 -1.204584  1.011151
3  1.455288  0.830276  0.822264 -0.385821
4  0.299957 -0.663821 -0.704605 -0.303356

2. Using major_axis

Data accessed  using panel.major_axis(index) method.
import pandas as pd
import numpy as np
insideaiml_data = {'key1' : pd.DataFrame(np.random.randn(5, 4)), 
   'key2' : pd.DataFrame(np.random.randn(5, 3))}
panelData = pd.Panel(insideaiml_data)
print(panelData.major_xs(1))
Its output is as follows −

        key1      key2
 0  1.417978  1.391994
 1  0.009445  0.375496
 2  0.834670 -0.677597
 3  0.121576       NaN

3. Using minor_axis

Data accessed using panel.minor_axis(index) method.

import pandas as pd
import numpy as np
insideaiml_data = {'key1' : pd.DataFrame(np.random.randn(5, 4)), 
   'key2' : pd.DataFrame(np.random.randn(5, 3))}
panelData = pd.Panel(insideaiml_data)
print(panelData.minor_xs(1))

Its output is as follows −
       key1      key2
0  0.325758 -1.526423
1  0.778292  0.936562
2  0.202640 -0.168996
3 -0.448355  0.210160
4  0.188512 -0.215780
I hope you enjoyed reading this article and finally, you came to know about Python Pandas - Panel
  
Liked what you read? Then don’t break the spree. Visit our insideAIML blog page to read more awesome articles. 
Or if you are into videos, then we have an amazing Youtube channel as well. Visit our InsideAIML Youtube Page to learn all about Artificial Intelligence, Deep Learning, Data Science and Machine Learning. 
Keep Learning. Keep Growing. 
  
Recommended course for you :
    
Recommended blogs for you :

Submit Review