0% found this document useful (0 votes)
16 views15 pages

Pandas DataFrame Methods Guide

The document provides an overview of various methods available in the Pandas DataFrame class, including head(), tail(), info(), count(), describe(), and nunique(). It explains how to access single columns, retrieve specific data, and rearrange columns within a DataFrame. Each method is accompanied by example code demonstrating its usage with a CSV file.

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)
16 views15 pages

Pandas DataFrame Methods Guide

The document provides an overview of various methods available in the Pandas DataFrame class, including head(), tail(), info(), count(), describe(), and nunique(). It explains how to access single columns, retrieve specific data, and rearrange columns within a DataFrame. Each method is accompanied by example code demonstrating its usage with a CSV file.

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

Data Science – Pandas – DataFrame – Methods

Contents
1. DataFrame Methods .......................................................................................................................... 2
1.1. head() ........................................................................................................................................... 2
1.2. tail() .............................................................................................................................................. 3
1.3. info() ............................................................................................................................................. 4
1.4. count() .......................................................................................................................................... 6
1.5. describe() ..................................................................................................................................... 8
1.6. nunique() ...................................................................................................................................... 9
2. Accessing single column From DataFrame ...................................................................................... 10
3. Rearranging columns in DataFrame ................................................................................................ 14

1|Page [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

9. PANDAS – DATAFRAME – METHODS

1. DataFrame Methods

 DataFrame is a predefined class.


 DataFrame having different methods.
 These methods perform an operation on DataFrame and returns result

1.1. head()

 head() is predefined method in DataFrame class.


 We can access head() method by using DataFrame object only.
 This method returns first five rows from the DataFrame

Program Accessing first five rows from DataFrame


Name [Link]
Input file [Link]

import pandas as pd

df1 = pd.read_csv("[Link]")
df2 = [Link]()

print(df2)

Output

2|Page [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

1.2. tail()

 tail() is predefined method in DataFrame class.


 We can access tail() method by using DataFrame object only.
 This method returns last five rows from the DataFrame

Program Accessing last five rows from DataFrame


Name [Link]
Input file [Link]

import pandas as pd

df1 = pd.read_csv("[Link]")
df2 = [Link]()

print(df2)

Output

3|Page [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

1.3. info()

 info() is predefined method in DataFrame class.


 We can access info() method by using DataFrame object only.
 This method returns below information about DataFrame,
o Type of object
o Range of object
o Number of columns
o Number of rows
o The data type of each column
o Number of data types
o Total memory usage

Program Accessing info() method from DataFrame


Name [Link]
Input file [Link]

import pandas as pd

df1 = pd.read_csv("[Link]")
[Link]()

Output

4|Page [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

Program Accessing info() method from DataFrame


Name [Link]
Input file sales1_with_nan.csv

import pandas as pd

df1 = pd.read_csv("sales1_with_nan.csv")
[Link]()

Output

5|Page [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

1.4. count()

 count() is predefined method in DataFrame class.


 We can access count() method by using DataFrame object only.
 This method returns number of non-null values from each column.

Program Accessing count() method from DataFrame


Name [Link]
Input file [Link]

import pandas as pd

df1 = pd.read_csv("[Link]")
c = [Link]()
print(c)

Output

6|Page [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

Program Accessing count() method from DataFrame


Name [Link]
Input file sales1_with_nan.csv

import pandas as pd

df1 = pd.read_csv("sales1_with_nan.csv")
c = [Link]()

print(c)

Output

7|Page [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

1.5. describe()

 describe() is predefined method in DataFrame class.


 We can access describe() method by using DataFrame object.
 This method returns the below values,
o count
o mean
o std
o min
o 25%
o 50%
o 75%
o max

Program Accessing describe() method from DataFrame


Name [Link]
Input file sales1_with_nan.csv

import pandas as pd

df1 = pd.read_csv("[Link]")
dsc = [Link]()

print(dsc)

Output

8|Page [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

1.6. nunique()

 nunique() is predefined method in DataFrame class.


 We can access nunique() method by using DataFrame object only.
 This method returns number of unique values from the DataFrame.

Program Get the number of nunique values from the DataFrame


Name [Link]

import pandas as pd

df1 = pd.read_csv("[Link]")
nu = [Link]()

print(nu)

Output

9|Page [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

2. Accessing single column From DataFrame

 We can access columns from the DataFrame,


o If we access single column then it returns the Series
o If we access two column then it returns the DataFrame with two
columns

Program Accessing single column from the DataFrame


Name [Link]
Input file [Link]

import pandas as pd

df = pd.read_csv("[Link]")

print([Link])

Output

10 | P a g e [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

Program Accessing single column from the DataFrame


Name [Link]
Input file [Link]

import pandas as pd

df = pd.read_csv("[Link]")

print(df["Product"])

Output

11 | P a g e [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

Program Accessing two column from the DataFrame


Name [Link]
Input file [Link]

import pandas as pd

df = pd.read_csv("[Link]")

print(df[["Customer Name", "Product"]])

Output

12 | P a g e [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

Program Accessing single column from the DataFrame, applying sum


Name [Link]
Input file [Link]

import pandas as pd

df = pd.read_csv("[Link]")
total = df['Quantity'].sum()

print(total)

Output

889

13 | P a g e [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

3. Rearranging columns in DataFrame

 We can rearrange columns in DataFrame


 We can customise the order of columns in DataFrame

Program Creating DataFrame by loading csv file


Name [Link]
Input file [Link]

import pandas as pd

df = pd.read_csv("[Link]")

print(df)

Output

14 | P a g e [Link] DATAFRAME METHODS


Data Science – Pandas – DataFrame – Methods

Program Rearranging columns in DataFrame


Name [Link]
Input file [Link]

import pandas as pd

df = pd.read_csv("[Link]")
df = df[["Product", "Customer Name", "Quantity", "Order ID"]]

print(df)

Output

15 | P a g e [Link] DATAFRAME METHODS

You might also like