Write a Python program to find the average of first row in a Panel


Assume, you have Panel and the average of the first row is,

Average of first row is:
Column1    0.274124
dtype: float64

Solution

To solve this, we will follow the steps given below −

  • Set data value as dictionary key is ‘Column1’ with value as pd.DataFrame(np.random.randn(5, 3))

data = {'Column1' : pd.DataFrame(np.random.randn(5, 3))}
  • Assign data to Panel and save it as p

p = pd.Panel(data)
  • Print the column using dict key Column1

print(p['Column1'])
  • Calculate theAverage of first row using, major_xs(0) ,

p.major_xs(0).mean()

Example

Let’s see the following code to get a better understanding −

import pandas as pd
import numpy as np
data = {'Column1' : pd.DataFrame(np.random.randn(5, 3))}
p = pd.Panel(data)
print("Panel values:")
print(p['Column1'])
print("Average of first row is:")
print(p.major_xs(0).mean())

Output

Panel values:
      0          1       2
0  0.629910 0.275741 -0.083281
1 -0.509143 -1.794204 0.300390
2 -1.944141 0.085508 -0.155167
3 1.551087 -0.671242 -0.838922
4 -0.643543 0.622777 1.112745
Average of first row is:
Column1    0.274124
dtype: float64

Updated on: 25-Feb-2021

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements