Python Pandas — Panel
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 −
Create Panel in Pandas
There are different ways to create Panel in pandas −
- From 3D ndarrays
- From the dictionary of DataFrames object
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
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
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
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
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
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.
For more information watch our Python Tutorial video.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…
Recommended course for you :