Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to locate rows and columns by using loc,iloc in python?

Description

To locate rows or columns to define X and y variable using python.

Process

loc:

  Used to get the rows and columns with particular labels from the index.

Iloc:

  Get rows and columns with particular positions in the index.

Sample Code

#import the libraries

import pandas as pd

#read the data set

data=pd.read_csv(‘/home/soft27/soft27

/Sathish/Pythonfiles/Employee.csv’)

#creating data frame

df=pd.DataFrame(data)

#first row of data frame

print(“****First row*******”)

print(df.iloc[0])

#second row of data frame

print(“***Second row********”)

print(df.iloc[1])

#last row of data frame

print(“***Last row********”)

print(df.iloc[-1])

#first column of data frame

print(“*****1st column******”)

print(df.iloc[:,0])

#second column of data frame

print(“****2nd column*******”)

print(df.iloc[:,1])

#last column of data frame

print(“****last column*******”)

print(df.iloc[:,-1])

Screenshots
locate rows and columns by using loc,iloc in python
import the libraries