0% found this document useful (0 votes)
24 views14 pages

Pandas - Dataframe - Attributes

The document provides an overview of various attributes of the Pandas DataFrame class, detailing how to access and utilize them. Key attributes include length, columns, shape, size, dtypes, and others, with examples of code demonstrating their usage. Each attribute serves a specific purpose in retrieving information about the DataFrame object.

Uploaded by

dheerajsai01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

Pandas - Dataframe - Attributes

The document provides an overview of various attributes of the Pandas DataFrame class, detailing how to access and utilize them. Key attributes include length, columns, shape, size, dtypes, and others, with examples of code demonstrating their usage. Each attribute serves a specific purpose in retrieving information about the DataFrame object.

Uploaded by

dheerajsai01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Data Science – Pandas – DataFrame – Attributes

Contents
1. DataFrame Attributes ........................................................................................................................ 2
1.1. Length of DataFrame ................................................................................................................... 2
1.2. columns ........................................................................................................................................ 3
1.3. shape ............................................................................................................................................ 4
1.4. shape[0] ....................................................................................................................................... 5
1.5. shape[1] ....................................................................................................................................... 6
1.6. size ............................................................................................................................................... 7
1.7. dtypes........................................................................................................................................... 9
1.8. empty ......................................................................................................................................... 10
1.9. index........................................................................................................................................... 12
1.10. values ....................................................................................................................................... 13
1.11. T................................................................................................................................................ 14

1|Page 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

8. PANDAS – DATAFRAME – ATTRIBUTES

1. DataFrame Attributes

 DataFrame is a predefined class.


 DataFrame having different attributes.
 These attributes return information about the DataFrame object.

1.1. Length of DataFrame

 We can check length of DataFrame by using len(p) function


 This function returns the total number of rows from the DataFrame

Program Checking total number of rows/length from the DataFrame


Name demo1.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print("Total number of rows in DataFrame:", len(df))

Output

Total number of rows in DataFrame: 600

2|Page 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

1.2. columns

 columns is predefined attribute in DataFrame class.


 We can access columns attribute by using DataFrame object.
 This attribute returns all column names from the DataFrame

Program Accessing columns attribute from DataFrame.


Name demo2.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print(df.columns)

Output

Index(['Order ID', 'Customer Name', 'Product', 'Quantity'],


dtype='object')

3|Page 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

1.3. shape

 shape is predefined attribute in DataFrame class.


 We can access shape attribute by using DataFrame object.
 This attribute returns the total number of rows and columns in tuple
format.
o From the tuple, first value represents total number of rows
o From the tuple, second value represents total number of columns

Program Accessing shape attribute from DataFrame.


Name demo3.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print(df.shape)

Output

(600, 4)

4|Page 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

1.4. shape[0]

 shape is predefined attribute in DataFrame class.


 We can access shape attribute by using DataFrame object.
 This attribute returns the total number of rows and columns in tuple
 Shape[0] returns total number for rows from the DataFrame

Program Accessing shape attribute and checking total number of rows


Name demo4.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print(df.shape[0])

Output

600

5|Page 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

1.5. shape[1]

 shape is predefined attribute in DataFrame class.


 We can access shape attribute by using DataFrame object.
 This attribute returns the total number of rows and columns in tuple
 Shape[1] returns total number for columns from the DataFrame

Program Accessing shape attribute and checking total number of columns


Name demo5.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print(df.shape[1])

Output

6|Page 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

1.6. size

 size is predefined attribute in DataFrame class.


 We can access size attribute by using DataFrame object.
 This attribute returns the total number of elements/values in DataFrame

Program Accessing total number of elements/values from DataFrame.


Name demo6.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print(df.size)

Output

2400

Make a note:

 size = Number of rows X Number of columns


 size = Row_count X Column_count

7|Page 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

Program Accessing total number of elements/values from DataFrame.


Name demo7.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print("Number of elements in DataFrame:", df.size)
print("Number of elements in DataFrame:",
df.shape[0]*df.shape[1])

Output

8|Page 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

1.7. dtypes

 dtypes is predefined attribute in DataFrame class.


 We can access dtypes attribute by using DataFrame object.
 This attribute returns the datatype of each column.

Program Checking all columns datatype from DataFrame


Name demo8.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print(df.dtypes)

Output

9|Page 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

1.8. empty

 empty is predefined attribute in DataFrame class.


 We can access empty attribute by using DataFrame object.
 This attribute check DataFrame is empty or not,
o If DataFrame is empty then it returns True other False.

Program Checking DataFrame empty or not


Name demo9.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print(df.empty)

Output

False

10 | P a g e 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

Program Checking DataFrame empty or not


Name demo10.py
Input file sales1.csv

import pandas as pd

df = pd.DataFrame()

print(df)
print()
print(df.empty)

Output

11 | P a g e 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

1.9. index

 index is predefined attribute in DataFrame class.


 We can access index attribute by using DataFrame object.
 This attribute return index start and end value from the DataFrame.

Program Accessing index attribute from DataFrame


Name demo11.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print(df.index)

Output

RangeIndex(start=0, stop=600, step=1)

12 | P a g e 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

1.10. values

 values is predefined attribute in DataFrame class.


 We can access values attribute by using DataFrame object.
 This attribute return values of DataFrame,
o Each row values in one array from starting to last row.

Program Accessing values attribute from DataFrame


Name demo12.py
Input file sales1.csv

import pandas as pd

df = pd.read_csv("sales1.csv")
print(df.values)

Output

13 | P a g e 8.PANDAS DATAFRAME ATTRIBUTES


Data Science – Pandas – DataFrame – Attributes

1.11. T

 T is predefined attribute in DataFrame class.


 We can access T attribute by using DataFrame object.
 T means its transpose, it returns rows as columns and columns as rows

Program Converting rows as column and columns as rows from DataFrame


Name demo13.py

import pandas as pd

details = [["Sagar", 20, 10000], ["Daniel", 16, 20000], ["Veeru", 24,


30000], ["Raju", 25, 40000], ["Kiran", 26, 50000], ["Kedar", 27,
60000], ["Reena", 28, 70000]]

df = pd.DataFrame(details, columns = ["Name", "Age", "Salary"])

print(df)
print()
print(df.T)

Output

14 | P a g e 8.PANDAS DATAFRAME ATTRIBUTES

You might also like