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 cellTo 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
lockeyword when modifying existing rows. Frequently, when modifying a dataframe withoutloc, you're dealing with a copy rather than the original data. For example:df[(df['col'] <= 5) & (df['col'] > 2)]["existing_col"] = new_valueHere, the condition returns a copy of the dataframe instead of the original data.
Challenge
EasyThe 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_VALIDandUTILIZATIONcolumns), whereIS_VALIDequals1andUTILIZATIONis smaller than0.4, to the valueMODIFIED.
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
4Data Analysis with Pandas
Descriptive StatisticsGrouping and Aggregating DataDifferent AggregationsMerge & Concat2Working with the DataFrame
Understanding DataFramesAccessing DataData Cleaning - Missing dataData Cleaning - More tools3Data Manipulation with Pandas
Return Requested ResultFilter DataAdd & DeleteModify DataModify StringsCustom Modifications