Saturday, September 25, 2021

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

Thursday, September 16, 2021

Python Video Classes

 

Python Video Class - 1


Python Video Class - 2


Python Video Class - 3


Python Video Class - 4


Python Video Class - 5


Python Video Class - 6


Python Video Class - 7


Python Video Class - 8


Python Video Class - 9


Python Video Class - 10

Series Creation in Pandas - 3

 Q - 1

Create an Empty Series using Pandas.


import pandas as pd

S=pd.Series()

print(S)


Output:


Series([], dtype: float64)



 Q - 2

Create a series using List.


import pandas as pd

L=[5,8,10,15,20]

S=pd.Series(L)

print(S)


Output:


0     5

1     8

2    10

3    15

4    20

dtype: int64



 Q - 3

Create a series using two different List.


import pandas as pd

Months=['Jan','Feb','Mar','Apr','May']

Days=[31,28,31,30,31]

S=pd.Series(Days,Months)

print(S)


Output:


Jan    31

Feb    28

Mar    31

Apr    30

May    31

dtype: int64



Q - 4

Create a series using range() method.


import pandas as pd

I=list(range(1,6))

D=list(range(2,11,2))

S=pd.Series(D,I)

print(S)


Output:

1     2

2     4

3     6

4     8

5    10

dtype: int64


Q - 5

Create a Series using range() and for loop.


import pandas as pd

I=[x for x in range(1,6)]

D=[x for x in range(3,16,3)]

S=pd.Series(D,I)

print(S)


Output:

1     3

2     6

3     9

4    12

5    15

dtype: int64



Q - 6

Handling floating point values for generating a Series.



import pandas as pd

I=[x for x in range(1,6)]

D=[2,5,8,4,9.6]

S=pd.Series(D,I)

print(S)


Output:

1    2.0

2    5.0

3    8.0

4    4.0

5    9.6

dtype: float64


Q - 7

Creating a Series using missing values (NaN)



import pandas as pd

import numpy as np

L=[2,3,5,7,8]

S=pd.Series([8,7.5,4.9,np.NaN,9])

print(S)


Output:


0    8.0

1    7.5

2    4.9

3    NaN

4    9.0

dtype: float64



Q - 8

Creating a Series and modify the index values.



import pandas as pd

S=pd.Series([31,28,31,30,31])

print(S)

S.index=['Jan','Feb','Mar','Apr','May']

print(S)


Output:

0    31

1    28

2    31

3    30

4    31

dtype: int64

Jan    31

Feb    28

Mar    31

Apr    30

May    31

dtype: int64



Q - 9

Creating a Series and access first and third element

import pandas as pd

D=[10,30,50,80,90]

S=pd.Series(D)

print(S)

print("First Element\n",S[0])

print("Third Element\n",S[2])


Output:


0    10

1    30

2    50

3    80

4    90

dtype: int64

First Element

 10

Third Element

 50

Q - 10

Creating a Series of 5 Elements and print complete Series, 2nd Element, and First 3 Element


import pandas as pd

D=[10,30,50,80,90]

S=pd.Series(D)

print(S)

print("Second Element\n",S[1])

print("First Three Element\n",S[0:3])

print("First Three Element\n",S[:3])


Output:


0    10

1    30

2    50

3    80

4    90

dtype: int64

Second Element

 30

First Three Element

 0    10

1    30

2    50

dtype: int64

First Three Element

 0    10

1    30

2    50

3    80

4    90

dtype: int64


Q - 11

Creating a Series of 5 Elements and perform slicing on Series


import pandas as pd

D=[10,20,30,40,50]

I=['a','b','c','d','e']

S=pd.Series(D,I)

print(S)

print(S['a'])

print(S[0:3])

print(S[-3:])


Output:

a    10
b    20
c    30
d    40
e    50
dtype: int64
10
a    10
b    20
c    30
dtype: int64
c    30
d    40
e    50
dtype: int64



Monday, April 12, 2021

Online Work Sheet Report

                                                     

Online Work Sheet Report

Session 2020 - 2021

 

Click Here To Fill Google Form