Program using Pandas and Numpy

Simple statistics using numpy and pandas

Statistical Aggregation using Numpy and Pandas

# Importing numpy and pandas
import numpy as np
import pandas as pd
# generating random values using numpy
random = np.random.RandomState(42)
# creating a pandas Series using random values
series = pd.Series(random.rand(10))
print(series)

#Output
0 0.374540
1 0.950714
2 0.731994
3 0.598658
4 0.156019
5 0.155995
6 0.058084
7 0.866176
8 0.601115
9 0.708073
# to sum all the random values of series
print(series.sum())

#Output(your output will come according to your random values)
5.201367359526748

# to get min value in the series
print(series.min())

#Output
0.05808361216819946

# to get maximum value in the series
print(series.max())

#Output
0.9507143064099162

# to get mean of the series
print(series.mean())

#Output
0.5201367359526748

# To get random values in a DataFrame using numpy
dataframe = pd.DataFrame({<strong>'A'</strong>: random.rand(10), random.rand(10)})
print(dataframe)

#Output
     A        B
0 0.020584 0.611853
1 0.969910 0.139494
2 0.832443 0.292145
3 0.212339 0.366362
4 0.181825 0.456070
5 0.183405 0.785176
6 0.304242 0.199674
7 0.524756 0.514234
8 0.431945 0.592415
9 0.291229 0.046450

# to sum all the values in dataframe
print(dataframe.sum())

#Output
A 3.952678
B 4.003872
dtype: float64

Previous||Next Chapter

2 Comments

  1. The creation of the dataframe didn’t work on my system I had to do this
    # To get random values in a DataFrame using numpy
    dataframe = pd.DataFrame({‘A’: random.rand(10), ‘B’:random.rand(10)})
    print(dataframe)

    But it doesn’t look the same , works but not the same visually

    • It will work, it’s very simple if you are new to this sometimes you will face problems, but you will learn by practising.

Leave a Reply