Pandas Basic tutorial 3 - Python Programming |
Open
Anaconda Navigator
Launch
Sypder
If you have not seen the Pandas
PROGRAM 1
import pandas as pd
a =
{'Name':pd.Series(['Somi','Sabha','Rakesh','Jwala','Shital','Swara','Kareena']),
'Age':pd.Series([15,16,15,16,16,16,16]),
'Rank':pd.Series([1,2,3,4,5,6,7])}
d = pd.DataFrame(a)
print ("DATA SERIES \n")
print (d)
print ("\n TRANSPOSE OF DATA SERIES \n")
print (d.T)
print ("\n ROW AND COLUMNS AXES LABELS \n")
print (d.axes)
print ("\n DATATYPES \n")
print (d.dtypes)
print ("\n ARE THE OBJECTS EMPTY OR NOT \n")
print (d.empty)
print ("\n DIMENSIONS OF THE OBJECT \n")
print (d.ndim)
print ("\n SHAPE OF THE OBJECT \n")
print (d.shape)
print ("\n TOTAL ELEMENTS IN THE OBJECT \n")
print (d.size)
print ("\n SIZE OF THE DATAFRAME \n")
print (d.values)
print ("\n FIRST 2 ROWS OF THE DATA FRAME \n")
print (d.head(2))
print ("\n LAST 2 ROWS OF THE DATA FRAME \n")
print (d.tail(2))
print("\n SUM \n")
print (d.sum())
print("\n SUM (1) \n")
print (d.sum(1))
print("\n MEAN \n")
print (d.mean())
print("\n MEDIAN \n")
print (d.median())
print("\n MODE \n")
print (d.mode())
print("\n STANDARD DEVIATION \n")
print (d.std())
print("\n MIN \n")
print (d.min())
print("\n MAX \n")
print (d.max())
print("\n SUMARY OF STATISTICS 1 \n")
print (d.describe())
print("\n SUMARY OF STATISTICS 2 \n")
print (d.describe(include=['object']))
print("\n SUMARY OF STATISTICS 3 \n")
print (d.describe(include='all'))
OUTPUT
DATA SERIES
Name Age Rank
0 Somi 15 1
1 Sabha 16 2
2 Rakesh 15 3
3 Jwala 16 4
4 Shital 16 5
5 Swara 16 6
6 Kareena 16 7
TRANSPOSE OF DATA SERIES
0 1 2 3 4 5 6
Name Somi Sabha Rakesh Jwala Shital
Swara Kareena
Age 15 16
15 16 16 16
16
Rank 1 2
3 4 5
6 7
ROW AND COLUMNS AXES LABELS
[RangeIndex(start=0, stop=7, step=1), Index(['Name', 'Age', 'Rank'], dtype='object')]
DATATYPES
Name object
Age int64
Rank int64
dtype: object
ARE THE OBJECTS EMPTY OR NOT
False
DIMENSIONS OF THE OBJECT
2
SHAPE OF THE OBJECT
(7, 3)
TOTAL ELEMENTS IN THE OBJECT
21
SIZE OF THE DATAFRAME
[['Somi' 15 1]
['Sabha' 16 2]
['Rakesh' 15 3]
['Jwala' 16 4]
['Shital' 16 5]
['Swara' 16 6]
['Kareena' 16 7]]
FIRST 2 ROWS OF THE DATA FRAME
Name Age Rank
0 Somi 15 1
1 Sabha 16 2
LAST 2 ROWS OF THE DATA FRAME
Name Age Rank
5 Swara 16 6
6 Kareena 16 7
SUM
Name SomiSabhaRakeshJwalaShitalSwaraKareena
Age
110
Rank
28
dtype: object
SUM (1)
0 16
1 18
2 18
3 20
4 21
5 22
6 23
dtype: int64
MEAN
Age 15.714286
Rank 4.000000
dtype: float64
MEDIAN
Age 16.0
Rank 4.0
dtype: float64
MODE
Name Age Rank
0 Jwala 16.0 1
1 Kareena NaN 2
2 Rakesh NaN 3
3 Sabha NaN 4
4 Shital NaN 5
5 Somi NaN 6
6 Swara NaN 7
STANDARD DEVIATION
Age 0.487950
Rank 2.160247
dtype: float64
MIN
Name Jwala
Age 15
Rank 1
dtype: object
MAX
Name Swara
Age 16
Rank 7
dtype: object
SUMARY OF STATISTICS 1
Age Rank
count 7.000000 7.000000
mean 15.714286 4.000000
std 0.487950 2.160247
min 15.000000 1.000000
25% 15.500000 2.500000
50% 16.000000 4.000000
75% 16.000000 5.500000
max 16.000000 7.000000
SUMARY OF STATISTICS 2
Name
count 7
unique 7
top Sabha
freq 1
SUMARY OF STATISTICS 3
Name Age Rank
count 7 7.000000 7.000000
unique 7 NaN
NaN
top Sabha NaN
NaN
freq 1 NaN
NaN
mean NaN 15.714286 4.000000
std NaN 0.487950 2.160247
min NaN 15.000000 1.000000
25% NaN 15.500000 2.500000
50% NaN 16.000000 4.000000
75% NaN 16.000000 5.500000
max NaN 16.000000 7.000000
PROGRAM
2
import pandas as pd
s = pd.Series(['Somi', 'Sabha', 'Rakesh Sharma', 'Satish Kapoor', '287', 'Anita','any@tty','934','SHAYNA'])
print("\n GIVEN SERIES \n")
print (s)
print("\n LOWER CASE \n")
print (s.str.lower())
print("\n UPPER CASE \n")
print (s.str.upper())
print("\n LENGTH \n")
print (s.str.len())
print("\n STRIP \n")
print (s.str.strip())
print("\n SPLIT \n")
print (s.str.split(' '))
print("\n CONCATENATE \n")
print (s.str.cat(sep='_'))
print("\n GET DUMMIES \n")
print (s.str.get_dummies())
print("\n CONTAINS \n")
print (s.str.contains(' '))
print("\n REPLACE \n")
print (s.str.replace('@','$'))
print("\n REPEAT \n")
print (s.str.repeat(2))
print("\n COUNT \n")
print (s.str.count('m'))
print("\n STARTS WITH\n")
print (s.str. startswith ('S'))
print("\n ENDS WITH \n")
print (s.str.endswith('a'))
print("\n FIND \n")
print (s.str.find('a'))
print("\n FIND ALL \n")
print (s.str.findall('a'))
print("\n SWAP CASE \n")
print (s.str.swapcase())
print("\n IS LOWER \n")
print (s.str.islower())
print("\n IS UPPER \n")
print (s.str.isupper())
print("\n IS NUMERIC \n")
print (s.str.isnumeric())
OUTPUT
GIVEN SERIES
0 Somi
1 Sabha
2 Rakesh Sharma
3 Satish Kapoor
4 287
5 Anita
6 any@tty
7 934
8 SHAYNA
dtype: object
LOWER CASE
0 somi
1 sabha
2 rakesh sharma
3 satish kapoor
4 287
5 anita
6 any@tty
7 934
8 shayna
dtype: object
UPPER CASE
0 SOMI
1 SABHA
2 RAKESH SHARMA
3 SATISH KAPOOR
4 287
5 ANITA
6 ANY@TTY
7 934
8 SHAYNA
dtype: object
LENGTH
0 4
1 5
2 13
3 13
4 3
5 5
6 7
7 3
8 6
dtype: int64
STRIP
0 Somi
1 Sabha
2 Rakesh Sharma
3 Satish Kapoor
4 287
5 Anita
6 any@tty
7 934
8 SHAYNA
dtype: object
SPLIT
0 [Somi]
1 [Sabha]
2 [Rakesh, Sharma]
3 [Satish, Kapoor]
4 [287]
5 [Anita]
6 [any@tty]
7 [934]
8 [SHAYNA]
dtype: object
CONCATENATE
Somi_Sabha_Rakesh Sharma_Satish Kapoor_287_Anita_any@tty_934_SHAYNA
GET DUMMIES
287 934 Anita Rakesh Sharma ... Sabha Satish Kapoor Somi any@tty
0 0 0 0
0 ...
0 0
1 0
1 0 0 0
0 ...
1 0
0 0
2 0 0 0
1 ...
0 0
0 0
3 0 0 0
0 ...
0 1
0 0
4 1 0 0
0 ...
0 0
0 0
5 0 0 1
0 ...
0 0
0 0
6 0 0 0
0 ...
0 0
0 1
7 0 1 0
0 ...
0 0
0 0
8 0 0 0
0 ...
0 0
0 0
[9 rows x 9 columns]
CONTAINS
0 False
1 False
2 True
3 True
4 False
5 False
6 False
7 False
8 False
dtype: bool
REPLACE
0 Somi
1 Sabha
2 Rakesh Sharma
3 Satish Kapoor
4 287
5 Anita
6 any$tty
7 934
8 SHAYNA
dtype: object
REPEAT
0 SomiSomi
1
SabhaSabha
2 Rakesh SharmaRakesh Sharma
3 Satish KapoorSatish Kapoor
4
287287
5
AnitaAnita
6 any@ttyany@tty
7
934934
8
SHAYNASHAYNA
dtype: object
COUNT
0 1
1 0
2 1
3 0
4 0
5 0
6 0
7 0
8 0
dtype: int64
STARTS WITH
0 True
1 True
2 False
3 True
4 False
5 False
6 False
7 False
8 True
dtype: bool
ENDS WITH
0 False
1 True
2 True
3 False
4 False
5 True
6 False
7 False
8 False
dtype: bool
FIND
0 -1
1 1
2 1
3 1
4 -1
5 4
6 0
7 -1
8 -1
dtype: int64
FIND ALL
0 []
1 [a, a]
2 [a, a, a]
3 [a, a]
4 []
5 [a]
6 [a]
7 []
8 []
dtype: object
SWAP CASE
0 sOMI
1 sABHA
2 rAKESH sHARMA
3 sATISH kAPOOR
4 287
5 aNITA
6 ANY@TTY
7 934
8 shayna
dtype: object
IS LOWER
0 False
1 False
2 False
3 False
4 False
5 False
6 True
7 False
8 False
dtype: bool
IS UPPER
0 False
1 False
2 False
3 False
4 False
5 False
6 False
7 False
8 True
dtype: bool
IS NUMERIC
0 False
1 False
2 False
3 False
4 True
5 False
6 False
7 True
8 False
dtype: bool