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

No comments:

Post a Comment