Monday, September 27, 2021

DataFrame in Python Class - 2

  DataFrame Attributes / Properties:


7. ndim:

Return the dimension of the DataFrame.

import pandas as pd

I=['IP','Bio','Chemistry','Physics','English']

D={"2018":[50,60,70,80.5,90],"2019":[40,35,45,55,32], \

   "2020":[65,75,85,45,52],}

df=pd.DataFrame(D,I)

df.index.name="Subject"

print(df)

print("Number of Dimension in Data Frame")

print(df.ndim)



Output:


           2018  2019  2020

Subject                    

IP         50.0    40    65

Bio        60.0    35    75

Chemistry  70.0    45    85

Physics    80.5    55    45

English    90.0    32    52

Number of Dimension in Data Frame

2



8. empty:

Check whether a DataFrame is Empty or Not.


import pandas as pd

import numpy as np

I=['IP','Bio','Chemistry','Physics','English']

D={"2018":[50,60,70,80.5,90],"2019":[40,35,45,np.NaN,32], \

   "2020":[65,75,85,45,52],}

df=pd.DataFrame(D,I)

df.index.name="Subject"

print(df)

print("Check whether a Data Frame is Empty or not")

print(df.empty)

print(df.isna())   # It is a function. Check NaN values in DataFrame

df1=pd.DataFrame()

print(df1)

print(df1.empty)


Output:

           2018  2019  2020

Subject                    

IP         50.0  40.0    65

Bio        60.0  35.0    75

Chemistry  70.0  45.0    85

Physics    80.5   NaN    45

English    90.0  32.0    52

Check whether a Data Frame is Empty or not

False

            2018   2019   2020

Subject                       

IP         False  False  False

Bio        False  False  False

Chemistry  False  False  False

Physics    False   True  False

English    False  False  False

Empty DataFrame

Columns: []

Index: []

True


9. count()

It is used to count the values in Rows and Columns of DataFrame.

df.count() : count the no. of values rowwise

df.count(0):count the no. of values rowwise

df.count(axis="rows"): count the no. of values rowwise

df.count(1): count the no. of values columnwise

df.count(axis='columns'): count the no. of values columnwise

import pandas as pd

import numpy as np

I=['IP','Bio','Chemistry','Physics','English']

D={"2018":[50,60,70,80.5,90],"2019":[40,35,45,np.NaN,32], \

   "2020":[65,75,85,45,52],}

df=pd.DataFrame(D,I)

df.index.name="Subject"

print(df)

print("Count the no. of values in rows")

print(df.count())

print("Count the no. of values in columns")

print(df.count(1))

print("Count the no. of values in rows")

print(df.count(0))

print("Count the no. of values in rows")

print(df.count(axis='rows'))

print("Count the no. of values in columns")

print(df.count(axis='columns'))



           2018  2019  2020

Subject                    

IP         50.0  40.0    65

Bio        60.0  35.0    75

Chemistry  70.0  45.0    85

Physics    80.5   NaN    45

English    90.0  32.0    52

Count the no. of values in rows

2018    5

2019    4

2020    5

dtype: int64

Count the no. of values in columns

Subject

IP           3

Bio          3

Chemistry    3

Physics      2

English      3

dtype: int64

Count the no. of values in rows

2018    5

2019    4

2020    5

dtype: int64

Count the no. of values in rows

2018    5

2019    4

2020    5

dtype: int64

Count the no. of values in columns

Subject

IP           3

Bio          3

Chemistry    3

Physics      2

English      3

dtype: int64


10. Transpose of Data Frame

import pandas as pd

import numpy as np

I=['IP','Bio','Chemistry','Physics','English']

D={"2018":[50,60,70,80.5,90],"2019":[40,35,np.NaN,55,32], \

   "2020":[65,75,85,45,52],}

df=pd.DataFrame(D,I)

df.index.name="Subject"

print(df)

print("Transpose of Data Frame")

print(df.T)


Output:


           2018  2019  2020

Subject                    

IP         50.0  40.0    65

Bio        60.0  35.0    75

Chemistry  70.0   NaN    85

Physics    80.5  55.0    45

English    90.0  32.0    52

Transpose of Data Frame

Subject    IP   Bio  Chemistry  Physics  English

2018     50.0  60.0       70.0     80.5     90.0

2019     40.0  35.0        NaN     55.0     32.0

2020     65.0  75.0       85.0     45.0     52.0

Saturday, September 25, 2021

Python Lect - 2

 

Python Version List

A list of Python versions with its released date is given below.

Python VersionReleased Date
Python 1.0January 1994
Python 1.5December 31, 1997
Python 1.6September 5, 2000
Python 2.0October 16, 2000
Python 2.1April 17, 2001
Python 2.2December 21, 2001
Python 2.3July 29, 2003
Python 2.4November 30, 2004
Python 2.5September 19, 2006
Python 2.6October 1, 2008
Python 2.7July 3, 2010
Python 3.0December 3, 2008
Python 3.1June 27, 2009
Python 3.2February 20, 2011
Python 3.3September 29, 2012
Python 3.4March 16, 2014
Python 3.5September 13, 2015
Python 3.6December 23, 2016
Python 3.7June 27, 2018
Python 3.8October 14, 2019
Python 3.9October 05, 2020
Python 3.10    October 04, 2021




What is an IDE?


An IDE (Integrated Development Environment) is a software application used by developers for creating programs.


List of Python IDE


1. PyCharm

2. Visual Studio Code

3. Sublime Text

4. Vim

5. Atom

6. Jupyter Notebook

7. Eclipse + PyDev + LiClipse

8. GNU Emacs

9. Spyder

10. Thonny

11. IDLE



Python Distributions

  • Anaconda Python
  • ActivePython
  • CPython
  • Enthought Canopy
  • WinPython



Important Python Libraries




1. Matplotlib

2. Pandas

3. Requests

4. NumPy

5. SQLAlchemy

6. BeautifulSoup

7. Pyglet

8. SciPy

9. Scrapy

10. PyGame

11. Python Twisted

12. Pillow

13. pywin32

14. wxPython

15. iPython

16. Nose

17. Flask

18. SymPy

19. Fabric

20. PyGTK

Python Lect - 1

 


What is Python?

  • Python is a high level programming language.
  • Python is a Dynamically Type programming language.
  • Python is a Strongly Type programming language.
  • Python is a Scripting Language
  • Python was created by Guido van Rossum, and released in 1991.
  • Python is a simple, general purpose, high level, and object-oriented programming language.
  • Python is an interpreted scripting language also.


Python is used for:
  • Web development (server-side),
  • Software development,
  • Mathematics,
  • System scripting.
  • Data Science
  • Date Mining
  • Desktop Applications
  • Console-based Applications
  • Mobile Applications
  • Software Development
  • Artificial Intelligence
  • Web Applications
  • Enterprise Applications
  • 3D CAD Applications
  • Machine Learning
  • Computer Vision or Image Processing Applications.
  • Speech Recognitions

Python Popular Frameworks and Libraries

Python has wide range of libraries and frameworks widely used in various fields such as machine learning, artificial intelligence, web applications, etc. We define some popular frameworks and libraries of Python as follows.

  • Web development (Server-side) - Django Flask, Pyramid, CherryPy
  • GUIs based applications - Tk, PyGTK, PyQt, PyJs, etc.
  • Machine Learning - TensorFlow, PyTorch, Scikit-learn, Matplotlib, Scipy, etc.
  • Mathematics - Numpy, Pandas, etc.


DataFrame in Python Class - 1

 DataFrame:

1. DataFrame is a 2 D Data Structure.

2. DataFrame is a 2 D Structure of Python Pandas Library.

3. DataFrame is a Heterogeneous Data Structure

4. It just like a table or spreadsheet.

5. It can contains 2 or more rows and columns.

6. Types of Columns can be different.

7. Size of DataFrame is Mutable.

8. Value of DataFrame also Mutable.

9. Arithmetic Operators can be performed on rows and columns.

10. It can store different types of values.

Example:

import pandas as pd

I=['A','B','C','D','E']

D={"2019":[50,60,70,80,90],"2020":[40,35,45,55,32]}

DF=pd.DataFrame(D,I)

print(DF)


Output:

   2019  2020
A    50    40
B    60    35
C    70    45
D    80    55
E    90    32


 DataFrame Attributes / Properties:

DataFrame has the following attributes:

1. index
2. columns
3. axes
4. dtypes
5. size
6. shape
7. ndim
8. empty
9. count
10. T


1. index

It display the index of the DataFrame.

import pandas as pd
I=['IP','Bio','Chemistry','Physics','English']
D={"2018":[50,60,70,80,90],"2019":[40,35,45,55,32], \
   "2020":[65,75,85,45,52],}
df=pd.DataFrame(D,I)
df.index.name="Subject"
print(df)
print("Index of Data Frame")
print(df.index)

Output:

           2018  2019  2020
Subject                    
IP           50    40    65
Bio          60    35    75
Chemistry    70    45    85
Physics      80    55    45
English      90    32    52
Index of Data Frame
Index(['IP', 'Bio', 'Chemistry', 'Physics', 'English'], dtype='object', name='Subject')


2. columns

It display the name of columns of DataFrame

import pandas as pd
I=['IP','Bio','Chemistry','Physics','English']
D={"2018":[50,60,70,80,90],"2019":[40,35,45,55,32], \
   "2020":[65,75,85,45,52],}
df=pd.DataFrame(D,I)
df.index.name="Subject"
print(df)
print("Columns of Data Frame")
print(df.columns)


Output:

           2018  2019  2020
Subject                    
IP           50    40    65
Bio          60    35    75
Chemistry    70    45    85
Physics      80    55    45
English      90    32    52
Columns of Data Frame
Index(['2018', '2019', '2020'], dtype='object')



3. Axes

It display both Index name and column name of DataFrame

import pandas as pd
I=['IP','Bio','Chemistry','Physics','English']
D={"2018":[50,60,70,80,90],"2019":[40,35,45,55,32], \
   "2020":[65,75,85,45,52],}
df=pd.DataFrame(D,I)
df.index.name="Subject"
print(df)
print("Axes of Data Frame")
print(df.axes)


Output:

          2018  2019  2020
Subject                    
IP           50    40    65
Bio          60    35    75
Chemistry    70    45    85
Physics      80    55    45
English      90    32    52
Axes of Data Frame
[Index(['IP', 'Bio', 'Chemistry', 'Physics', 'English'], dtype='object', name='Subject'), Index(['2018', '2019', '2020'], dtype='object')]


4. dtype:

Display the data type of columns/Values

import pandas as pd
I=['IP','Bio','Chemistry','Physics','English']
D={"2018":[50,60,70,80.5,90],"2019":[40,35,45,55,32], \
   "2020":[65,75,85,45,52],}
df=pd.DataFrame(D,I)
df.index.name="Subject"
print(df)
print("Data Type of Data Frame")
print(df.dtypes)



Output:


           2018  2019  2020
Subject                    
IP         50.0    40    65
Bio        60.0    35    75
Chemistry  70.0    45    85
Physics    80.5    55    45
English    90.0    32    52
Data Type of Data Frame
2018    float64
2019      int64
2020      int64
dtype: object

5.size

Display the size of DataFrame i.e. total number of elements.

import pandas as pd
I=['IP','Bio','Chemistry','Physics','English']
D={"2018":[50,60,70,80.5,90],"2019":[40,35,45,55,32], \
   "2020":[65,75,85,45,52],}
df=pd.DataFrame(D,I)
df.index.name="Subject"
print(df)
print("size of Data Frame")
print(df.size)

Output:


           2018  2019  2020
Subject                    
IP         50.0    40    65
Bio        60.0    35    75
Chemistry  70.0    45    85
Physics    80.5    55    45
English    90.0    32    52
size of Data Frame
15

6.shape

Display number  of rows and columns
import pandas as pd
I=['IP','Bio','Chemistry','Physics','English']
D={"2018":[50,60,70,80.5,90],"2019":[40,35,45,55,32], \
   "2020":[65,75,85,45,52],}
df=pd.DataFrame(D,I)
df.index.name="Subject"
print(df)
print("Shape of Data Frame")
print(df.shape)

Output:

           2018  2019  2020
Subject                    
IP         50.0    40    65
Bio        60.0    35    75
Chemistry  70.0    45    85
Physics    80.5    55    45
English    90.0    32    52
Shape of Data Frame
(5, 3)

Friday, September 24, 2021

iloc[ ] and loc[ ] in Series

 iloc[ ] and loc[ ] in Series

Indexing and accessing can also be done using iloc[ ]  and loc [ ] methods.

iloc[ ]  - iloc is used for indexing or selecting based on position.

Friday, September 17, 2021

Series Creation in Pandas - 4

 Q:1 

Create a Series and Explain the use of head() and tail()


import pandas as pd

D=[10,20,30,40,50,60,70,80,90]

I=['a','b','c','d','e','f','g','h','i']

S=pd.Series(D,I)

print(S)

print(S.head())  #print first 5 elements

print(S.tail())     #print last 5 elements

print(S.head(3))    #print first 3 elements

print(S.tail(3))    #print last elements

print(S.head(-2))   #print first n-2 elements (not print last 2 elements)

print(S.tail(-2))   #print last n-2 elements (not print first 2 elements)


Output:

a    10
b    20
c    30
d    40
e    50
f    60
g    70
h    80
i    90
dtype: int64
a    10
b    20
c    30
d    40
e    50
dtype: int64
e    50
f    60
g    70
h    80
i    90
dtype: int64
a    10
b    20
c    30
dtype: int64
g    70
h    80
i    90
dtype: int64
a    10
b    20
c    30
d    40
e    50
f    60
g    70
dtype: int64
c    30
d    40
e    50
f    60
g    70
h    80
i    90
dtype: int64