Add, Assign, And Modify Values In DataFrame

Introduction

 
You must have learned and created your own DataFrames by now (refer to my Creating Dataframes in Pandas) using its various attributes (refer to my article- Dataframes Attributes in Pandas). You also must have done your hands-on accessing data in those DataFrames. If not, don't worry! It’s never too late (refer to my article- Accessing Data in DataFrame)
 
Now its time to play with data in Pandas’ DataFrames. In this article, we will learn,
  • How to add particular value in a particular place within a DataFrame.
  • How to assign a particular value to a specific row or a column in a DataFrame.
  • How to add new rows and columns in DataFrame.
  • How to update or modify a particular value.
  • How to update or modify a particular row or a column.

Modify a single value

  • If you want to modify a single value with specific column and row name then you must follow:
    SYNTAX: dataFrameObject.column_name[row_to_be_changed] = replace_with_ value
  • If you want to modify a single value with specific index then you must follow,
    SYNTAX: dataFrameObject.iloc[row_index, column_index] = replace_with_value
    1. import pandas as pd    
    2.     
    3. dict= {'English':[85,73,98], 'Math':[60,80,58], 'Science':[90,60,74], 'French': [95,87,92] }    
    4.     
    5. df=pd.DataFrame(dict,index=['2018','2019','2020'])    
    6. print(df)    
    7. print('\n')    
    8.     
    9. print('Modify a single value:')    
    10. print('\n')    
    11. using_name=df.Math['2019']=99    
    12. print(df)    
    13. print('\n')    
    14.     
    15. print('Modify a single value using index:')    
    16. print('\n')    
    17. using_index=df.iloc[2,2]=99    
    18. print(df)    
Output
 

Add/Modify a Column

  • If you want to add a whole new column with the same values, you must follow:

    SYNTAX: dataFrameObject[column_to_be_changed] = replace_with_columnName
    1. import pandas as pd    
    2.     
    3. dict= {'English':[85,73,98], 'Math':[60,80,58], 'Science':[90,60,74], 'French': [95,87,92] }    
    4.     
    5. df=pd.DataFrame(dict,index=['2018','2019','2020'])    
    6. print(df)    
    7. print('\n')    
    8.     
    9. print('Adding new column:')    
    10. print('\n')    
    11. df['Economics']=99    
    12. print(df)    
Output
 
 
If you want to modify any column’s values or even if you want to add a column with different values, then you have various methods to do so:
  • Just add a list (Method 1)
    SYNTAX: dataFrameObject[column_to_be_changed] = [list_of_ columnName _to_replace_with]

  • Using keyword at (Method 2)
    SYNTAX: dataFrameObject.at [: , column_to_be_changed] = [list_of_ columnName _to_replace_with]

  • Using keyword loc (Method 3)
    SYNTAX: dataFrameObject.loc [: , column_to_be_changed] = [list_of_ columnName _to_replace_with]

  • Using another dataFrame to assign values (Method 4)
    SYNTAX: dataFrameObject1= dataFrameObject2.assign (column_to_be_changed = [list_of_ columnName _to_replace_with])
  1. import pandas as pd    
  2.     
  3. dict= {'English':[85,73,98], 'Math':[60,80,58], 'Science':[90,60,74], 'French': [95,87,92] }    
  4.     
  5. df=pd.DataFrame(dict,index=['2018','2019','2020'])    
  6. df['Economics']=99    
  7. print(df)    
  8. print('\n')    
  9.     
  10. print('Method 1:')    
  11. print('\n')    
  12. df['Economics']=[99,85,56]    
  13. print(df)    
  14. print('\n')    
  15.     
  16. print('Method 2:')    
  17. print('\n')    
  18. df.at[:,'Economics']=[78,85,74]    
  19. print(df)    
  20. print('\n')    
  21.     
  22. print('Method 3:')    
  23. print('\n')    
  24. df.loc[:,'Economics']=[94,87,86]    
  25. print(df)    
  26. print('\n')    
  27.     
  28. print('Method 4:')    
  29. print('\n')    
  30. df2=df.assign(Economics=[92,81,66])    
  31. print(df2)   
Output
 
 

Add/Modify a Row

 
If you want to add a new row, you can follow 2 different ways:
  • Using keyword at,
    SYNTAX: dataFrameObject.at [new_row. :] = new_row_value

  • Using keyword loc,
    SYNTAX: dataFrameObject.loc [new_row. :] = new_row_value
Using the above syntax, you would add a new row with the same values. If you want to add different values in the particular row corresponding to each column, then add the list of values (same as we learned while adding/modifying a column).
  1. import pandas as pd    
  2.     
  3. dict= {'English':[85,73,98], 'Math':[60,80,58], 'Science':[90,60,74], 'French': [95,87,92] }    
  4.     
  5. df=pd.DataFrame(dict,index=['2018','2019','2020'])    
  6. print(df)    
  7. print('\n')    
  8.     
  9. print('Adding new row using AT:')    
  10. print('\n')    
  11. df.at[2021,:]=100    
  12. print(df)    
  13. print('\n')    
  14.     
  15. print('Adding new row using LOC:')    
  16. print('\n')    
  17. df.loc['2022',:]=[89,21,87,59]    
  18. print(df)    
Output
 
 

Deleting Columns

  • To delete a single column, we use the keyword ‘del’ or we can also use method drop().
    SYNTAX - del dataFrameObject[column_to_be_deleted]
  • To delete multiple columns, you must use method drop(), you can use it to delete a single value as well.
    SYNTAX - dataFrameObject.drop([column_to_be_deleted],axis=1,inplace=True)
Note
The drop() method contains various parameters, a few of them are the axis and in place which we are using. So, we must know their use as well. When we use axis=1 it refers to columns, when axis=0 is used it refers to rows.
 
Inplace is a Boolean parameter which is by default False if used as it returns a copy but is True then performs the operation inplace and returns nothing.
  1. import pandas as pd    
  2.     
  3. dict= {'English':[85,73,98], 'Math':[60,80,58], 'Science':[90,60,74], 'French': [95,87,92] }    
  4.     
  5. df=pd.DataFrame(dict,index=['2018','2019','2020'])    
  6. df['Economics']=99    
  7. print(df)    
  8. print('\n')    
  9.     
  10. print('Deleting single column:')    
  11. print('\n')    
  12. del df['Economics']    
  13. print(df)    
  14. print('\n')    
  15.     
  16. print('Deleting multiple columns:')    
  17. print('\n')    
  18. df.drop(['English','Science'],axis=1,inplace=True)    
  19. print(df)    
Output
 
 
Note
We can also use this syntax for deleting multiple columns:
 
df.drop(df.column[[0,2]],axis=1,inplace=True)
 

Deleting Rows

 
Deletion of a row or be it multiple rows is similar to that of the columns, we use the drop() method with similar syntax. Let's understand this directly with an example:
  1. import pandas as pd    
  2.     
  3. dict= {'English':[85,73,98], 'Math':[60,80,58], 'Science':[90,60,74], 'French': [95,87,92] }    
  4.     
  5. df=pd.DataFrame(dict,index=['2018','2019','2020'])    
  6. df.at[2021,:]=100    
  7. df.at[2022,:]=[99,98,97,96]    
  8. print(df)    
  9. print('\n')    
  10.     
  11. print('Deleting a row:')    
  12. print('\n')    
  13. df.drop(['2019'],axis=0,inplace=True)    
  14. print(df)    
  15. print('\n')    
  16.     
  17. print('Deleting multiple rows:')    
  18. print('\n')    
  19. df.drop(df.index[[0,2]],axis=0,inplace=True)    
  20. print(df)     
Output
 
 

Summary

 
In this article, we learned about adding, modifying, updating, and assigning values in a DataFrame.Also, you are now aware of how to delete values or rows and columns in a DataFrame. We will learn about more things in my series of articles of PANDAS. Practice hard!
 
In my next article, we will learn about “Different Operations on data in DataFrames”.
 
Feedback or queries related to this article are most welcome.
 
Thanks for reading!


Similar Articles