Signup/Sign In

Basic Operation on Panel

A panel allows numerous operations to be done on its data. Some of the most basic operations which we learn in any data structure are the selection, addition, and deletion of data. In this section, we will throw light on each one of them.

1. Selection of Panel items

Any data structure is ranked by its efficiency to manipulate data and the first step towards doing so is the selection of items. Panels provide us with comprehensive operations to select the desired items by using the correct parameters.

studyTonight_pan = pd.Panel(np.random.randn(5,10,5),
                        items = ['A' ,'B' ,'C', 'D','E'],
                        major_axis = pd.date_range('1/2/2019', periods=10),
                        minor_axis = ['1','2','3','4','5'])

print(studyTonight_pan['A'])

Output:

2. Addition of Panel Items

Another important part of data manipulation and analysis is the addition of new data, or old data at a different location. Any data structure should be efficient in doing so. Panels offer us an easy way to add the required items to the existing one.

studyTonight_pan['C'] = studyTonight_pan['A']+studyTonight_pan['B']
studyTonight_pan['C']

Output:

3. Deletion of Panel Items

Whenever there is any item which is out of place, or an item which we don’t need at all, the need arises for it to be deleted because it may mess with our results; not to mention the additional storage space it will consume. Panels offers us an easy way to delete the items we want without affecting the other items.

del studyTonight_pan['D']
studyTonight_pan

Output:

4. Panel transpose

One of the important functions involved in data manipulation is the transpose function. This function basically switches the axes of the panel in the manner we want it to. This is very useful when we have to move the data sets around regularly.

Before that, as we have done a lot of operations on the previous panel, we make a new one now.

studyTonight_pan = pd.Panel(np.random.randn(6,5,6),
                       items=['o' ,'p' ,'q', 'r','s','t'],
                       major_axis=pd.date_range('1/1/2019', periods=5),
                       minor_axis=['5','6','7','8','9','10'])
print(studyTonight_pan)

Output:

Now let's transpose it,

studyTonight_pan.transpose(1, 2, 0)

5. Data selection from a Panel

Do not have this confused with section 7a of our tutorial. That was the selection of items while this is the selection of data. As we have mentioned several times in this article, a data structure is as good as its data handling capacity. Panel takes the complex process of selecting data from a specific axis and brings it down to one line. Here we will see how (but before that we have to create another panel for clarity of function use):

# insert a code for creating a new panel here if required

a. Selection of an item

We can select individual items with a simple line of code by putting in the right parameters.

studyTonight_pan['o']

Output:

Error 404: Soul not found

b. Selecting a Major Axis

Similarly, we can select a major axis just with one line of code and the right parameters.

studyTonight_pan.major_xs(studyTonight_pan.major_axis[3])

Output:

1 2 3 4
J 0.550053 0.956819 -0.400523 -0.583020
K 0.836906 1.066107 -1.876660 0.044376
L -2.781268 1.682333 0.462075 -0.151477
M -0.536076 0.085945 -0.475268 -0.547530

c. Selecting a Minor Axis

Lastly, we can select a minor axis just with one line of code and the right parameters.

studyTonight_pan.minor_xs('7')

Output:

6. Deprecating a Panel

Pandas is a fast-growing library with an ever-increasing versatility. The efficient routines for the functioning and indexing of DataFrames, Series, and Panels have resulted in the complexity of codes. Nowadays a 3D Panel is quite uncommon when compared to a 2D DataFrame or a 1D Series for data analysis. The xarray package was developed especially for the support of the multidimensional analysis of a panel. Therefore we sometimes deprecate a panel to an xarray or a Multi-Index DataFrame.

a. Deprecating to a Multi-Index DataFrame

studyTonight_pan.to_frame()

Output:

Conclusion

In this tutorial, we have learned about the various ways of creating a panel, the parameters required for doing so, performing basic operation on a panel, and how to deprecate an existing panel. If you have any queries regarding panels, feel free to comment in the comment section below.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.