Skip to content

================ by Jawad Haider

Chpt 2 - Data Manipulation with Pandas

03 -Operations on Data in Pandas


import numpy as np
import pandas as pd
rng=np.random.RandomState(42)
ser=pd.Series(rng.randint(0,10,4))
ser
0    6
1    3
2    7
3    4
dtype: int64
df=pd.DataFrame(rng.randint(0,10,(3,4)),columns=['A',"B",'C','D'])
df
A B C D
0 6 9 2 6
1 7 4 3 7
2 7 2 5 4
np.exp(ser)
0     403.428793
1      20.085537
2    1096.633158
3      54.598150
dtype: float64
np.sin(df*np.pi/4)
A B C D
0 -1.000000 7.071068e-01 1.000000 -1.000000e+00
1 -0.707107 1.224647e-16 0.707107 -7.071068e-01
2 -0.707107 1.000000e+00 -0.707107 1.224647e-16
area = pd.Series({'Alaska': 1723337, 'Texas': 695662,
'California': 423967}, name='area')
population = pd.Series({'California': 38332521, 'Texas': 26448193,
'New York': 19651127}, name='population')
area
Alaska        1723337
Texas          695662
California     423967
Name: area, dtype: int64
population
California    38332521
Texas         26448193
New York      19651127
Name: population, dtype: int64
population/area
Alaska              NaN
California    90.413926
New York            NaN
Texas         38.018740
dtype: float64
area.index | population.index
/tmp/ipykernel_15930/3572280633.py:1: FutureWarning: Index.__or__ operating as a set operation is deprecated, in the future this will be a logical operation matching Series.__or__.  Use index.union(other) instead.
  area.index | population.index

Index(['Alaska', 'California', 'New York', 'Texas'], dtype='object')