How To Use Panels In Pandas Example

Pandas panel structure comes from the word panel data, but it is only applicable to pandas versions before 0.25. Since pandas version 0.25, the panel structure has been deprecated. This article will give you some examples about the pandas’ panel structure, wish it is helpful when you need it.

1. What Is Pandas Panel Structure.

  1. The panel is a three-dimensional data structure used to store data.
  2. It has three axes: items (axis 0), major_axis (axis 1), and minor_axis (axis 2).
  3. items: axis = 0, each item in the panel corresponds to a DataFrame object.
  4. major_axis: axis = 1, used to describe the row index of each DataFrame.
  5. minor_axis: axis = 2, used to describe the column index of each DataFrame.

2. How To Create Pandas Panel Object.

  1. You can create a panel using the following constructor.
    pandas.Panel(data, items, major_axis, minor_axis, dtype, copy)
    
    data : Input data can be ndarray, series, list, dictionary, or dataframe object.
    
    items : axis=0.
    
    major_axis : axis=1.
    
    minor_axis : axis=2.
    
    dtype : Data type of each column.
    
    copy : The default is false, indicating whether to copy data or not.
  2. Create an empty panel.
    # import pandas module as pd.
    import pandas as pd
    
    # create an empty panel.
    panel_object = pd.Panel()
    
    # print out the panel object.
    print(panel_object)
  3. Create a Panel from a 3-dimension array.
    # import pandas module as pd
    import pandas as pd
    
    # import numpy module as np
    import numpy as np
    
    # Returns a uniformly distributed random sample with values between [0,1], a 3 dimension array.
    data = np.random.rand(2,4,5)
    
    # create the pandas panel object.
    panel_object = pd.Panel(data)
    
    # print out the panel object.
    print (panel_object)
  4. Create a Panel from a DataFrame object.
    # import pandas, numpy module.
    import pandas as pd
    import numpy as np
    
    # create a python dictionary object the key is panel item, and the value is a pandas DataFrame object.
    data = {'Item1' : pd.DataFrame(np.random.randn(6, 3)),
       'Item2' : pd.DataFrame(np.random.randn(8, 2))}
    
    # create the panel object based on the above DataFrame dictionary.
    panel_object = pd.Panel(data)
    
    # print out the panel object.
    print(panel_object)

3. How To Query Pandas Panel Data.

  1. If you want to select data from the panel object, you can use the three axes of the panel, that is, items, major_axis, and minor_ axis.
  2. The following example uses items to query data.
    # import pandas, numpy module.
    import pandas as pd
    import numpy as np
    
    # create a python dictionary object the key is panel item, and the value is a pandas DataFrame object.
    data = {'Item1' : pd.DataFrame(np.random.randn(6, 3)),
       'Item2' : pd.DataFrame(np.random.randn(8, 2))}
    
    # create the panel object based on the above DataFrame dictionary.
    panel_object = pd.Panel(data)
    
    # print out the panel object.
    print(panel_object)
    
    # print the item1 in panel object.
    print(panel_object['Item1'])

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.