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
- import pandas as pd
- dict= {'English':[85,73,98], 'Math':[60,80,58], 'Science':[90,60,74], 'French': [95,87,92] }
- df=pd.DataFrame(dict,index=['2018','2019','2020'])
- print(df)
- print('\n')
- print('Modify a single value:')
- print('\n')
- using_name=df.Math['2019']=99
- print(df)
- print('\n')
- print('Modify a single value using index:')
- print('\n')
- using_index=df.iloc[2,2]=99
- 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
- import pandas as pd
- dict= {'English':[85,73,98], 'Math':[60,80,58], 'Science':[90,60,74], 'French': [95,87,92] }
- df=pd.DataFrame(dict,index=['2018','2019','2020'])
- print(df)
- print('\n')
- print('Adding new column:')
- print('\n')
- df['Economics']=99
- 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])
- import pandas as pd
- dict= {'English':[85,73,98], 'Math':[60,80,58], 'Science':[90,60,74], 'French': [95,87,92] }
- df=pd.DataFrame(dict,index=['2018','2019','2020'])
- df['Economics']=99
- print(df)
- print('\n')
- print('Method 1:')
- print('\n')
- df['Economics']=[99,85,56]
- print(df)
- print('\n')
- print('Method 2:')
- print('\n')
- df.at[:,'Economics']=[78,85,74]
- print(df)
- print('\n')
- print('Method 3:')
- print('\n')
- df.loc[:,'Economics']=[94,87,86]
- print(df)
- print('\n')
- print('Method 4:')
- print('\n')
- df2=df.assign(Economics=[92,81,66])
- print(df2)





Aashina AroraPosted Feb 12, 2022, 9:21 AM
Hi Amit, You can use- dictionary_name["P_WEBSTORE"] = 'UIY"
Amit KaundalPosted Jan 11, 2022, 10:46 AM
Hi Ashina how to update the value if we have large json. For example ['city'] = ['Rome', {'P_ACCOUNT_PARTY_CREATION': 'PERSON', 'P_WEBSTORE': 'API'}, 'Athens'] . In this we have column = city and 3 values in it. Now if i want to change the 'P_WEBSTORE': 'API' = 'P_WEBSTORE': 'UIY' , how i can do that.