How to select rows and columns by name or index in dataframes python
In this tutorial, You will learn how to select rows and columns by name or index in dataFrame using loc & iloc | Python Pandas
Pandas has provided iloc and loc functions to select rows and columns. These functions are very helpful in data preprocessing for data science and machine learning projects.
Syntax : pandas.DataFrame.iloc[<row selection>, <column selection>]
For Rows: pandas.DataFrame.iloc[row_number]
For Columns: pandas.DataFrame.iloc[:,column_number]
import pandas as pd
data = pd.DataFrame({
'name':['ravi','david','raju','david','kumar','teju'],'id':[6,1,2,8,3,4],
'salary':[15000,20000,30000,45389,50000,20000],
'year' :[2017,2017,2018,2018,2019,2018]
})
#printing dataframe
print(data)
Output:
id name salary year 0 6 ravi 15000 2017 1 1 david 20000 2017 2 2 raju 30000 2018 3 8 david 45389 2018 4 3 kumar 50000 2019 5 4 teju 20000 2018
#how to select single first row
print(data.iloc[1])
Output:
id 1 name david salary 20000 year 2017 Name: 1, dtype: object
#how to select first row datafrme
print(data.iloc[0])
id 6 name ravi salary 15000 year 2017 Name: 0, dtype: object
#How to select last row from dataframe
print("\nSelect last row\n", data.iloc[-1])
Output:
Select last row id 4 name teju salary 20000 year 2018 Name: 5, dtype: object
#How to select multiple rows from dataframe
print("\nSelect multiple rows\n",data.iloc[2:4])
Output:
Select multiple rows id name salary year 2 2 raju 30000 2018 3 8 david 45389 2018
Now times comes from columns, It’s very important in data science in preprocessing. Mostly data have multiple columns like features, We have to select some of the features only. We can do with iloc function.
#How to select single column from dataframe
print("\nSelect single column from data\n",data.iloc[:,1])
Output:
Select single column from data 0 ravi 1 david 2 raju 3 david 4 kumar 5 teju Name: name, dtype: object
#How to select first column from dataframe
print("\nSelect first column from data\n",data.iloc[:,0])
Output:
Select first column from data 0 6 1 1 2 2 3 8 4 3 5 4 Name: id, dtype: int64
#How to select last column from dataframe
print("\nSelect last column from data\n",data.iloc[:,-1])
Output:
Select last column from data 0 2017 1 2017 2 2018 3 2018 4 2019 5 2018 Name: year, dtype: int64
#How to select multiple columns from dataframe
print("\nSelect multiple columns from data\n",data.iloc[:,1:3])
Output:
Select multiple columns from data name salary 0 ravi 15000 1 david 20000 2 raju 30000 3 david 45389 4 kumar 50000 5 teju 20000