Access Data From DataFrame In Python


In this article, I have explained the different ways of accessing data from DataFrames in Python. Before reading this article, I will suggest you read the previous article - 
Now, let's understand how can we access the values of a DataFrame. 
We can access data of DataFrames in many ways,
  1. Value
  2. Columns
  3. Rows
We are creating a DataFrame using the following commands. Then, we will see how we can access its rows, columns, & values and understand how it can be done in different ways.
  1. ##Creating DataFrame    
  3. import pandas as pd    
  4. dictObj = {'EmpId' : ['E01','E02','E03','E04'],    
  5.        'EmpName' : ['Raj','Atul','Reena','Ayushi'],    
  6.        'Department' : ['IT','IT','HR','Accounts']}    
  7. df=pd.DataFrame(dictObj, index=['First','Second','Third','Fourth'])    
  9. ##Output    
  10.                   EmpId     EmpName        Department    
  11. First              E01       Raj               IT    
  12. Second             E02       Atul              IT    
  13. Third              E03       Reena             HR    
  14. Fourth             E04       Ayushi         Accounts   


We can access the individual value of DataFrame in the following ways.

By using row name and row index number

Using the row name and row index number along with the column, we can easily access a single value of a DataFrame.
  1. Syntax : <DataFrame Object> . <column name> [<row name/ row index number>]   
  1. Example :  
  2. df.EmpName[‘Third’]   ##Access using row name    
  3. ##Output  Reena    
  5. df.EmpName[2]        ## Access using row index    
  6. ##Output Reena    
Accessing Value of a DataFrame

By using at and iat attributes

We can also access a single value of a DataFrame with the help of “at” and “iat” attributes.
 Syntax  Purpose
 <DataFrame Object>.at [<row name>,<column name>]  Access a single value by row/column name.
 <DataFrame Object>.iat [<row index no>,<column index no>]  Access a single value by row/column index no
  2. ##Output   Atul  
  4. df.iat[2,2]  
  5. ## Output   HR  
Accessing Value by at and iat from DataFrame in Python
At and iat take two arguments. If we pass only one argument, then it will generate an error. See the below picture for error.
Error in iat and at

Accessing Single Column

We can also access column of a DataFrame by using the below syntax,
  1. <DataFrame Object> [ <Column Name> ]  
  2. <DataFrame Object> . <Column Name>  
  1. df [‘EmpName’]    ## First Way  
  3. df.EmpName    ## Second Way  
Accessing Column from DataFrame
Accessing Multiple Columns
We can also access multiple columns of a DataFrame by passing a list of columns name inside the square bracket.
  1. Syntax : <DataFrame Object> [ [<Column Name1> , <Column Name2>,<Column Name3> ]]  
  3. Example : df [ [‘EmpName’,’Department’] ]  
Accessing Multiple Columns from DataFrame

Accessing Rows

We can access rows of a DataFrame in two ways.

By using loc and iloc

We can access a single row and multiple rows of a DataFrame with the help of “loc” and “iloc”.
<DataFrame Object>.loc [ [ <row name>] ]
Access a single row or multiple rows by name.
<DataFrame Object>.iloc [ [<row index no> ] ]
Access a single or multiple rows by row index no
  1. df.loc[[‘Second’]]   ## Access row using location, pass row name  
  2. df.iloc[[2]]  ## Access row using row index number  
Accessing Rows from DataFrame in Python

Different forms of “loc” and “iloc”

By using loc and iloc we can also access subset of a dataframe or we can say slicing the dataframe. Syntax :
  1. <DataFrame Object> . loc [<start row name>:<end row name> , <start column name>:<end column name> ]  
  2. <DataFrame Object> . iloc [<start row number>:<end row number> , <start column number>:<end column number> ]  
Examples of Slicing
df.loc [ : ]
Fetching all rows and columns from dataframe
df.loc [‘First’ : , :]
df.iloc [ 0 : , : ]
Fetching all row starting from ‘First’ and fetching all columns.
df.loc [ : ‘Third’ , : ]
df.iloc [ : 3, : ]
Fetching rows starting from ‘First’ till the ‘Third’ with all columns.
df.loc [‘Second’ : ’Third’ , : ]
df.iloc [1:3, : ]
Fetching row starting from ‘Second’ and end to ‘Third’ with all columns.
df.loc [ : , ’EmpName’ : ]
df.iloc [ : , 1 : ]
Fetching all rows with ‘EmpName’ column to end column.
df.loc [ : , : ‘EmpName’]
df.iloc [: , : 2]
Fetching all rows till column EmpName.
df.loc [‘Second’ : ’Third’ , ‘EmpName’ : ‘Department’]
df.iloc [1:3, 1:3]
Fetching rows starting with ‘Second’ till ‘Third’ and columns from ‘EmpName’ till ‘Department’.
From the above articles, I hope now you can access any value of DataFrame as per requirement easily. Now, the question is how we can add and modifying data values of a DataFrame.

Adding a Row


at and loc

We already discuss about the “at” and “loc” attribute for accessing a single value. However, “at” and “loc” attribute is also used to add a new row in DataFrame.
  1. ##Syntax :  
  2. <DataFrame Object> . at [<row index name> ] = <values>  
  3. <DataFrame Object> . loc [<row index name> ] = <values>  
  5. ##Example:  
  6.[‘Fifth’, : ] = [‘E05’,’Nakul’,’HR’]  
  8.[‘Seventh’,’EmpName’:’Department’] = [‘Vipul’,’IT’]  
Adding Row into DataFrame

If we don’t pass the column value then it automatically takes ‘NaN’. Like in the above example we don't pass EmpId for Seventh row, then it takes it as NaN.

Modifying a Row

To change the value of a row, we can use the below syntax.
  1. <DataFrame Object>.<Column Name> [<row name>] = <new value>  ##Syntax  
  3. df.EmpId[‘Seventh’]=’E07’   ##Example 
Modifying a Row in DataFrame

Adding a column

It is very easy to add a column into an existing DataFrame. We can use the below syntax for adding a column into DataFrame.
  1. <DataFrame Object> [<Column Name>]  ##Syntax  
  2. ##Examples  
  3. df[‘City’]  ## added a new column with ‘NaN’ as default value.  
  4. df[‘City’]=’New Delhi’ 
Adding Column into DataFrame

Deleting a Column

Del is used to delete a column from DataFrame.
  1. del <DataFrame Object> [<Column name>]   ##Syntax  
  3. del df ['City']  ##Example 
Delete Column from DataFrame


Now, we have learned how we can access the data from DataFrames in Python. I hope after reading this article, you can easily access any value, rows, and columns from DataFrame. All the queries related to this article and sample files are always welcome. Thanks for reading.!!!