Access dataframe in python class 12 for the subject Informatics practices where you will learn about the 6 easy ways to select or access dataframe data in python. So here we start!

Access dataframe in python class 12 IP

Pandas Python to Dataframe Operations allows delete, rename, head and hail functions. In the previous topic, we discussed how to add columns, rows in data frames. Click here to read the post. In this post, we will discuss more dataframe operations.

How do you access DataFrame values in Python?

You can select or access data from dataframes in following 6 easy ways:

  1. Select/Access column using [column_name]
  2. Select/Access column using dot (.) notation
  3. Select/Access row/column using loc[]
  4. Select/Access row/column using iloc[]
  5. Select/Access row/column using a slice
  6. Select/Access individual value

All of the above operations we will discuss one by one with good examples. So let’s begin! The first way to access dataframe in python class 12 is using column name.

Select/Access column using [column_name]

Observe the following codes for select/access column using column name. Observe the following code and outputs.

Code 1:

import pandas as pd
def df_operations():
    sc_4yrs={2016:{'Virat Kohli':2595,'Rohit Sharma':2406,'Shikhar Dhawan':2378},
         2017:{'Virat Kohli':2818,'Rohit Sharma':2613,'Shikhar Dhawan':2295},
         2018:{'Virat Kohli':2595,'Rohit Sharma':2406,'Shikhar Dhawan':2378},
         2019:{'Virat Kohli':2595,'Rohit Sharma':2406,'Shikhar Dhawan':2378}}
    df=pd.DataFrame(sc_4yrs)
    print(df[2018])
df_operations()
select data using column name
select data using column name

Code 2:

import pandas as pd
def df_operations():
    sc_4yrs={2016:{'Virat Kohli':2595,'Rohit Sharma':2406,'Shikhar Dhawan':2378},
         2017:{'Virat Kohli':2818,'Rohit Sharma':2613,'Shikhar Dhawan':2295},
         2018:{'Virat Kohli':2595,'Rohit Sharma':2406,'Shikhar Dhawan':2378},
         2019:{'Virat Kohli':2595,'Rohit Sharma':2406,'Shikhar Dhawan':2378}}
    df=pd.DataFrame(sc_4yrs)
    print(df[[2017,2019]])
df_operations()
select or access dataframe in python class 12
select or access dataframe in python class 12

You can access any values with square brackets and column names as used in the above code. In the first code, a single column is accessed within square brackets. When you want to access multiple columns use multiple square brackets and separate the column names with a comma.

The second way to access dataframe in python class 12 is using dot notation.

Select/Access column using dot (.) notation

You can access any column using . notation. Observe the code:

import pandas as pd
def df_operations():
    dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
         'InternalMarks':[18,19,20,18,19],
         'Annual Exam':[76,78,80,76,73]})
    df=pd.DataFrame(dt)
    print(df.Name)
df_operations()
output of select column using dot notation
output of select column using dot notation

While using dot notation with column names you cannot print or access the columns which contains a space in column name.

The third way to access dataframe in python class 12 is using loc[] attribute.

You can watch this video for more understanding:

Select/Access row/column using loc[]

You can use loc[] to select or access row or column using loc[]. Let’s we see the way in this section of access dataframe in python class 12. Observe the code:

Code 1:

import pandas as pd
def df_operations():
    dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
         'InternalMarks':[18,19,20,18,19],
         'Annual Exam':[76,78,80,76,73]})
    df=pd.DataFrame(dt)
    print(df.loc[0:1,:])
df_operations()

Code 2:

import pandas as pd
def df_operations():
    dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
         'InternalMarks':[18,19,20,18,19],
         'AnnualExam':[76,78,80,76,73]})
    df=pd.DataFrame(dt)
    print(df.loc[:,'Name':'AnnualExam'])
df_operations()

When multiple column names used within loc[] it will display all columns falling in between the columns.

Code 3:

import pandas as pd
def df_operations():
    dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
         'InternalMarks':[18,19,20,18,19],
         'AnnualExam':[76,78,80,76,73]})
    df=pd.DataFrame(dt)
    print(df.loc[0:1,'Name':'AnnualExam'])
df_operations()

Observe the output yourself.

The next method to access dataframe in python class 12 is using iloc[].

Watch this video lesson:

Select/Access row/column using iloc[]

iloc[] – i refers to integer location of row/column in dataframe, observe this code and check your output.

import pandas as pd
def df_operations():
    dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
         'InternalMarks':[18,19,20,18,19],
         'AnnualExam':[76,78,80,76,73]})
    df=pd.DataFrame(dt)
    print(df.iloc[0:2,0:2])
df_operations()

In iloc[] the integer location displays the rows and columns falling between start index value and end index value.

Select/Access row/column using a slice

The slicing can be also used to select or access row or column. Have look at this code:

import pandas as pd
def df_operations():
    dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
         'English':[74,79,48,53,68],
         'Physics':[76,78,80,76,73],
         'Chemistry':[57,74,55,89,70],
         'Biology':[76,85,63,68,59],
         'IP':[82,93,69,98,79]})
    df=pd.DataFrame(dt)
    print(df[1:4])
df_operations()

In the above code, I used slice starting from index 1 to 4 which fetch records with all columns falling between the given index. You can use different slicing variations for the same.

Follow this link to access NCERT Solutions Chapter 2 Data handling Using Pandas I:

NCERT Solution Chapter 2 Data Handling Using Pandas I

Select/Access individual value

You can access individual value using the index value for the dataframe element. Observe this code:

Code 1:

import pandas as pd
def df_operations():
    dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
         'English':[74,79,48,53,68],
         'Physics':[76,78,80,76,73],
         'Chemistry':[57,74,55,89,70],
         'Biology':[76,85,63,68,59],
         'IP':[82,93,69,98,79]})
    df=pd.DataFrame(dt)
    print(df.Name[2])
df_operations()

The output of the above code is: Chetan

Code 2:

import pandas as pd
def df_operations():
    dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
         'English':[74,79,48,53,68],
         'Physics':[76,78,80,76,73],
         'Chemistry':[57,74,55,89,70],
         'Biology':[76,85,63,68,59],
         'IP':[82,93,69,98,79]})
    df=pd.DataFrame(dt)
    print(df.at[1,'Physics'])
df_operations()

The output of the above code is: 78

Code 3:

import pandas as pd
def df_operations():
    dt=({'Name':['Akshit','Bharat','Chetan','Dhaval','Gaurang'],
         'English':[74,79,48,53,68],
         'Physics':[76,78,80,76,73],
         'Chemistry':[57,74,55,89,70],
         'Biology':[76,85,63,68,59],
         'IP':[82,93,69,98,79]})
    df=pd.DataFrame(dt)
    print(df.iat[1,3])
df_operations()

The output of the above code is: 74

Watch this video for more understanding regarding filter data from the data frame according to conditions:

Follow this link for important questions based on this topic:

Important Questions and Answers Select or Access values from Dataframe

If you are looking for practical programs based on dataframe follow this link:

Practical Programs on Dataframe Class 12 IP

I hope you understood the concept well. In the next post in this section, we will discuss deleting and modifying values in dataframe. 

Thank you for reading the post. If you have any queries or doubts feel free to contact us through the contact us page. If you like the post share the post and comment your views on this post.

Follow this link access complete study material for Class 12:

Informatics Practices Class 12 Study Material

One thought on “Access dataframe in python class 12 IP – The 6 easy ways”

Leave a Reply