Access Data From DataFrame In Python

In this article, I am going to explain how you can access value, rows, and columns of a 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    
  2.     
  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'])    
  8.     
  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   

Value

 
We can access an 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 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    
  4.     
  5. df.EmpName[2]        ## Access using row index    
  6. ##Output Reena    
Output
 
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
 
Example
  1. df.at[‘Second’,’EmpName’]  
  2. ##Output   Atul  
  3.   
  4. df.iat[2,2]  
  5. ## Output   HR  
Accessing Value by at and iat from DataFrame in Python
 
Note
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>  
Like
  1. df [‘EmpName’]    ## First Way  
  2.   
  3. df.EmpName    ## Second Way  
Output
 
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> ]]  
  2.   
  3. Example : df [ [‘EmpName’,’Department’] ]  
Output
 
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”.
 
Syntax
Purpose
<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
slicing
df.loc [‘First’ : , :]
or
df.iloc [ 0 : , : ]
Fetching all row starting from ‘First’ and fetching all columns.
df.loc [ : ‘Third’ , : ]
or
df.iloc [ : 3, : ]
Fetching rows starting from ‘First’ till the ‘Third’ with all columns.
df.loc [‘Second’ : ’Third’ , : ]
or
df.iloc [1:3, : ]
Fetching row starting from ‘Second’ and end to ‘Third’ with all columns.
df.loc [ : , ’EmpName’ : ]
or
df.iloc [ : , 1 : ]
Fetching all rows with ‘EmpName’ column to end column.
df.loc [ : , : ‘EmpName’]
or
df.iloc [: , : 2]
Fetching all rows till column EmpName.
df.loc [‘Second’ : ’Third’ , ‘EmpName’ : ‘Department’]
or
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>  
  4.   
  5. ##Example:  
  6. df.at[‘Fifth’, : ] = [‘E05’,’Nakul’,’HR’]  
  7. df.at[‘Sixth’]=[‘E06’,’Rahul’,’Accounts’]  
  8. df.at[‘Seventh’,’EmpName’:’Department’] = [‘Vipul’,’IT’]  
Output
 
Adding Row into DataFrame
 
Note
If we don’t pass column value then it automatically it takes ‘NaN’. Like in 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  
  2.   
  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  
  2.   
  3. del df ['City']  ##Example 
Output
 
Delete Column from DataFrame
 

CONCLUSION

 
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.!!!