Header Ads Widget

Responsive Advertisement

Class 12 Subjecr Code : 065 : Operation on the DataFrame


Operations on rows and columns in DataFrames We can perform some basic operations on rows and columns of a DataFrame like selection, deletion, addition, and renaming, as discussed in this section.

import pandas as pd

print("How to access the elements from dataframe")

student={"Name": ['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}

df3=pd.DataFrame(student)

print(df3)




Adding a New Column to a DataFrame We can easily add a new column to a DataFrame. Let us consider the DataFrame df3 defined earlier. In order to add a new column for another student ‘Punit’, we can write the following statement:

import pandas as pd

print("how to add new columns in dataframe")

student={"Name": ['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}

df3["Physics"]=[23,45,49,49,49,50]

print(df3) 



 
We can also add new columns in dataframe using insert function. Here we define the syntax of the insert function . Using this function we can add any column at any particular position. Here the syntax

import pandas as pd

student={"Name":['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}

df3=pd.DataFrame(student,index=["A","B","C","D","E"])

print("how to add new columns in dataframe")

print("How to insert New columns at perticular location")

df3.insert(3,'Maths',[23,45,49,49,49])

print(df3) 


We can also change data of an entire column to a particular value in a DataFrame. For example, the following statement sets ECO=50 for all subjects for the column name 'ECO':

import pandas as pd
student={"Name":['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}
df3=pd.DataFrame(student)
print("how to add new columns in dataframe")
df3["Physics"]=[23,45,49,49,49]
df3["ECO"]=50
print(df3)

 

 We can add a new row to a DataFrame using the DataFrame.loc[ ] method. Consider the DataFrame ResultDF that has three rows for the three subjects – Maths, Science and Hindi. Suppose, we need to add the marks for English subject in ResultDF, we can use the following statement: 


import pandas as pd

student={"Name":['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}

df3=pd.DataFrame(student)

print("how to add new columns in dataframe")

print("Adding value to series")

df3.loc[6]=["PUNIT",56,67]

print(df3) 



We can add new columns in the dataframe we can use following syntax

           import pandas as pd

student={"Name":['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}
df3=pd.DataFrame(student)
print("how to add new columns in dataframe")
print("How to insert New columns at perticular location")
df3.insert(3,'Maths',[23,45,49,49,49])
print(df3)

 



NOTE : If we try to add a row with lesser values than the number of columns in the DataFrame, it results in a ValueError, with the error message: ValueError: Cannot set a row with mismatched columns. Similarly, if we try to add a column with lesser values than the number of rows in the DataFrame, it results in a ValueError, with the error message: ValueError: Length of values does not match length of index.


Adding a New Row to a DataFrame 
We can add a new row to a DataFrame using the DataFrame.loc[ ] method. 

import pandas as pd
student={"Name":['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}
df3=pd.DataFrame(student,index=["A","B","C","D","E"])
print("how to add new columns in dataframe")
print("How to insert New columns at perticular location")
df3.loc['F'] = ["Rajat",85, 86]
print(df3)



 

If we try to add a row with lesser values than the number of columns in the DataFrame, it results in a ValueError, with the error message: ValueError: Cannot set a row with mismatched columns. 

Similarly, if we try to add a column with lesser values than the number of rows in the DataFrame, it results in a ValueError, with the error message: ValueError: Length of values does not match length of index. 

Further, we can set all values of a DataFrame to a particular value, for example: 

import pandas as pd

student={"Name":['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}

df3=pd.DataFrame(student,index=["A","B","C","D","E"])

print("how to add new columns in dataframe")

print("How to insert New columns at perticular location")

df3[: ] = 0 

print(df3)



Deleting Rows or Columns from a DataFrame 
We can use the DataFrame.drop() method to delete rows and columns from a DataFrame. We need to specify the names of the labels to be dropped and the axis from which they need to be dropped. To delete a row, the parameter axis is assigned the value 0 and for deleting a column,the parameter axis is assigned the value 1. 
Consider the following DataFrame: 

import pandas as pd

student={"Name":['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}
df3=pd.DataFrame(student,index=["A","B","C","D","E"])

print("How to delete particular row and multiple row from the dataframe")
df3 = df3.drop('A', axis=0)
print(df3)
df3 = df3.drop(['C',"B"], axis=0)
print(df3)

 


Deleting Particular columns from data frame 

The following example shows how to delete the columns having labels "Name"

import pandas as pd

student={"Name":['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}

df3=pd.DataFrame(student,index=["A","B","C","D","E"])

print("How to delete particular row and multiple row from the dataframe")

df3 = df3.drop("Name", axis=1)

print(df3)




The following example shows how to delete the Particular row having labels "A"

import pandas as pd
student={"Name":['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}
df3=pd.DataFrame(student,index=["A","B","C","D","E"])
print("How to delete particular row and multiple row from the dataframe")

df3 = df3.drop(["A"], axis=0)
print("After deleting Row\n",df3)
df3 = df3.drop(["E","C"], axis=0)
print("After deleting Multiple Row\n",df3)


 
Renaming the name of the columns  and index name. we use rename function to change the name of the data frame columns and index names. We can change the labels of rows and columns in a DataFrame using the DataFrame.rename() method. Consider the following DataFrame 

import pandas as pd
student={"Name":['Ram','shyaam','Hari',"Aditya","pankaj"],"Eco":[50,48,49,50,49],"IP":[50,50,50,50,50]}
df3=pd.DataFrame(student,index=["A","B","C","D","E"])
print("How to change columns name and index names",df3)
df3.rename({"Name":"Stu_Name","Eco":"Eco_marks","IP":"IP_marks"},axis="columns", inplace=True)
print("After deleting Row\n",df3)

 



Boolean Indexing Boolean means a binary variable that can represent either of the two states - True (indicated by 1) or False (indicated by 0). In Boolean indexing, we can select the subsets of data based on the actual values in the DataFrame rather than their row/column labels. Thus, we can use conditions on column names to filter data values. Consider the DataFrame ResultDF, the following statement displays True or False depending on whether the data value satisfies the given condition or not. 











Joining, Merging and Concatenation of DataFrames 
(A) Joining We can use the pandas.DataFrame.append() method to merge two DataFrames. It appends rowsof the second DataFrame at the end of the first DataFrame. Columns not present in the first DataFrame are added as new columns. For example, consider the two DataFrames— dFrame1 and dFrame2described below. Let us use theappend() method to append dFrame2 to dFrame1:

Post a Comment

0 Comments