Modifying a Specific DataFrame Row

Write a Pandas program to change the score in row ‘d’ to 11.5.

Example 1:

Input: DataFrame with 'score' column and row indexed as 'd' 
Output: DataFrame with updated score for row 'd'

Example 2:

Input: DataFrame with 'score' column and row indexed as 'd' 
Output: DataFrame with updated score for row 'd'

Use the pandas DataFrame loc[] function.

import pandas as pd

def change_score(df):
    df.loc['d', 'score'] = 11.5
    return df

df1 = pd.DataFrame({'name': ['Tom', 'Jack', 'Steve', 'Ricky'], 'score': [85, 90, 87, 91]}, index=['a', 'b', 'c', 'd'])
df2 = pd.DataFrame({'name': ['Adam', 'Eve', 'Stark', 'Rogers'], 'score': [88, 92, 95, 97]}, index=['a', 'b', 'c', 'd'])

print(change_score(df1))
print(change_score(df2))

 

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!