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.

##Creating DataFrame    
import pandas as pd    
dictObj = {'EmpId' : ['E01','E02','E03','E04'],    
       'EmpName' : ['Raj','Atul','Reena','Ayushi'],    
       'Department' : ['IT','IT','HR','Accounts']}    
df=pd.DataFrame(dictObj, index=['First','Second','Third','Fourth'])    
                  EmpId     EmpName        Department    
First              E01       Raj               IT    
Second             E02       Atul              IT    
Third              E03       Reena             HR    
Fourth             E04       Ayushi         Accounts   

Accessing DataFrame Value

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.

Syntax: <DataFrame Object> . <column name> [<row name/ row index number>]   


df.EmpName[‘Third’]   ##Access using row name    
##Output  Reena    
df.EmpName[2]        ## Access using row index    
##Output Reena    


Accessing DataFrame Value

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

##Output   Atul  
## 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 DataFrame Column

We can also access column of a DataFrame by using the below syntax,

<DataFrame Object> [ <Column Name> ]  
<DataFrame Object> . <Column Name>  


df [‘EmpName’]    ## First Way  
df.EmpName    ## Second Way  


Accessing Column from DataFrame

Accessing Multiple DataFrame Columns

We can also access multiple columns of a DataFrame by passing a list of columns name inside the square bracket.

Syntax : <DataFrame Object> [ [<Column Name1> , <Column Name2>,<Column Name3> ]]    


df [ [‘EmpName’,’Department’] ]  


Accessing Multiple Columns from DataFrame

Accessing DataFrame 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
df.loc[[‘Second’]]   ## Access row using location, pass row name  
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 :

<DataFrame Object> . loc [<start row name>:<end row name> , <start column name>:<end column name> ]  
<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 DataFrame 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.

##Syntax :  
<DataFrame Object> . at [<row index name> ] = <values>  
<DataFrame Object> . loc [<row index name> ] = <values>  
##Example:[‘Fifth’, : ] = [‘E05’,’Nakul’,’HR’][‘Sixth’]=[‘E06’,’Rahul’,’Accounts’][‘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 DataFrame Row

To change the value of a row, we can use the below syntax.

<DataFrame Object>.<Column Name> [<row name>] = <new value>    

Modifying a Row in DataFrame

Adding a DataFrame 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.

<DataFrame Object> [<Column Name>]  
df[‘City’]  ## added a new column with ‘NaN’ as default value.  
df[‘City’]=’New Delhi’ 

Adding Column into DataFrame

Deleting a DataFrame Column

Del is used to delete a column from DataFrame.

del <DataFrame Object> [<Column name>]   
del df ['City']  


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

Similar Articles