Menu
Coddy logo textTech

Modify Data

Lesson 11 of 19 in Coddy's Pandas Analytics course.

To override the data of a specific cell, column, or row, you can assign a new value to it:

df["existing_col"] = new_value            # Mofidy column
df.loc[index] = new_value                 # Modify row
df.loc[index, "existing_col"] = new_value # modify cell

To update the column value of rows that meet a specific condition use the same condition, we learned in previous lessons with the .loc method:

df.loc[df['col'] > 5, 'existing_col'] = new_value
 
df.loc[(df['col'] <= 5) & (df['col'] > 2), 'existing_col'] = new_value

It is important to use the loc keyword when modifying existing rows. Frequently, when modifying a dataframe without loc, you're dealing with a copy rather than the original data. For example:

df[(df['col'] <= 5) & (df['col'] > 2)]["existing_col"] = new_value

Here, the condition returns a copy of the dataframe instead of the original data.

challenge icon

Challenge

Easy

The CSV file stats.csv contains information about stats.

Here is the first 5 lines of the file:

ID,COUNTRY,COLOR,SKILL,SKILL_POINTS,UTILIZATION,IS_VALID,CATEGORY
1,France,Signal violet,marksmanship,14,0.1924,1,SHOW
2,Solomon Islands,Pearl violet,crocheting,4,0.6108,1,TREE
3,Germany,Bottle green,calligraphy,12,0.88646,0,SHOW
4,Mauritania,Fawn brown,paper cutting,9,0.058,1,JAPE
  • Modify all cells (except the cells in the IS_VALID and UTILIZATION columns), where IS_VALID equals 1 and UTILIZATION is smaller than 0.4, to the value MODIFIED.

Store the result in the df variable.

Try it yourself

# pandas as pd is already imported
df = pd.read_csv("./stats.csv")
for column in df.columns:
    if column not in ["IS_VALID", "UTILIZATION"]:
        df[column] = df[column].astype(object)
# Write your code below

All lessons in Pandas Analytics