Introduction
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,
- Value
- Columns
- 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'])
##Output
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>]
Example
df.EmpName[‘Third’] ##Access using row name
##Output Reena
df.EmpName[2] ## Access using row index
##Output Reena
Output
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
df.at[‘Second’,’EmpName’]
##Output Atul
df.iat[2,2]
## Output HR
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.
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>
Like
df [‘EmpName’] ## First Way
df.EmpName ## Second Way
Output
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> ]]
Example
df [ [‘EmpName’,’Department’] ]
Output
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
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’ : , :]
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 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:
df.at[‘Fifth’, : ] = [‘E05’,’Nakul’,’HR’]
df.at[‘Sixth’]=[‘E06’,’Rahul’,’Accounts’]
df.at[‘Seventh’,’EmpName’:’Department’] = [‘Vipul’,’IT’]
Output
Note
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.
##Syntax
<DataFrame Object>.<Column Name> [<row name>] = <new value>
##Example
df.EmpId[‘Seventh’]=’E07’
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.
##Syntax
<DataFrame Object> [<Column Name>]
##Examples
df[‘City’] ## added a new column with ‘NaN’ as default value.
df[‘City’]=’New Delhi’
Deleting a DataFrame Column
Del is used to delete a column from DataFrame.
##Syntax
del <DataFrame Object> [<Column name>]
##Example
del df ['City']
Output
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.!!!