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)
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 pdstudent={"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"]=50print(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.
import pandas as pdstudent={"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)
import pandas as pdstudent={"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)
import pandas as pdstudent={"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)
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)
0 Comments