Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Updating DataFrame Values

import pandas as pd
houses = pd.read_csv("data/kc_house_data.csv")
titanic = pd.read_csv("data/titanic.csv")
netflix = pd.read_csv("data/netflix_titles.csv", sep="|", index_col=0)
btc = pd.read_csv("data/coin_Bitcoin.csv")
countries = pd.read_csv("data/world-happiness-report-2021.csv", index_col=0)

Rename

countries
Loading...
mapper = {"Regional indicator": "regional_indicator", "Ladder score": "ladder_score"}
countries.rename(columns=mapper).head()
Loading...
mapper = {"Finland": "finland", "Denmark": "denmark"}
countries.rename(index=mapper).head()
Loading...

Replace

titanic
Loading...
titanic["sex"] = titanic["sex"].replace("female", "F") 
titanic
Loading...
titanic["sex"] = titanic["sex"].replace("F", "female") 
titanic
Loading...
titanic["sex"].replace("male", "M")
0 female 1 M 2 female 3 M 4 female ... 1304 female 1305 female 1306 M 1307 M 1308 M Name: sex, Length: 1309, dtype: object
titanic["sex"].replace(["female", "male"], ["F", "M"])
0 F 1 M 2 F 3 M 4 F .. 1304 F 1305 F 1306 M 1307 M 1308 M Name: sex, Length: 1309, dtype: object
titanic
Loading...
titanic["age"].replace("?", "IDK").value_counts()
age IDK 263 24 47 22 43 21 41 30 40 ... 60.5 1 74 1 0.4167 1 11.5 1 26.5 1 Name: count, Length: 99, dtype: int64
titanic["age"].replace("?", None).value_counts()
age 24 47 22 43 21 41 30 40 18 39 .. 60.5 1 74 1 0.4167 1 11.5 1 26.5 1 Name: count, Length: 98, dtype: int64
titanic["age"].replace(["?"], [None]).value_counts(dropna=False)
age None 263 24 47 22 43 21 41 30 40 ... 60.5 1 74 1 0.4167 1 11.5 1 26.5 1 Name: count, Length: 99, dtype: int64
titanic["age"] = titanic["age"].replace(["?"], [None])
titanic["age"].value_counts(dropna=False)
age None 263 24 47 22 43 21 41 30 40 ... 60.5 1 74 1 0.4167 1 11.5 1 26.5 1 Name: count, Length: 99, dtype: int64

Updating With .loc[ ]

countries
Loading...
countries.loc["Finland", ["Ladder score", "Healthy life expectancy"]]
Ladder score 7.842 Healthy life expectancy 72.0 Name: Finland, dtype: object
countries.loc[["Costa Rica", "Panama", "Panama", "Honduras"], ["Ladder score", "Healthy life expectancy"]]
Loading...
countries.loc[["Denmark", "Sweden", "Norway"], ["Regional indicator"]]
Loading...
countries.loc[["Denmark", "Sweden", "Norway"], ["Regional indicator"]] = "Scandinavia"
countries.head(10)
Loading...
countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
/tmp/ipykernel_29376/4182435422.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'LOL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  countries.loc["Finland"] = "LOL"
countries.loc["Finland", ["upperwhisker", "lowerwhisker"]] = "MEOW"
countries
Loading...
countries.loc[["Finland", "Denmark", "Iceland", "Sweden", "Norway"]]
Loading...
countries.loc[["Finland", "Denmark", "Iceland", "Sweden", "Norway"], ['Regional indicator', 'isCold']] = "DOGS!!!"
countries.loc[["Finland", "Denmark", "Iceland", "Sweden", "Norway"]]
Loading...
countries.loc[["Finland", "Denmark", "Iceland", "Sweden", "Norway"], ['Regional indicator', 'isCold']] = ["Nordic Countries", "yes!" ]
many_bedrooms = houses["bedrooms"] >= 10
houses.loc[many_bedrooms, ['bedrooms']]
Loading...
houses.loc[many_bedrooms, ['bedrooms']] = "TOO MANY!"
/tmp/ipykernel_29376/1323911148.py:1: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value 'TOO MANY!' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.
  houses.loc[many_bedrooms, ['bedrooms']] = "TOO MANY!"
houses[many_bedrooms]
Loading...
houses
Loading...
high_quality = houses["grade"] > 12
great_views = houses["view"] == 4
houses['luxury_level'] = "normal"
houses
Loading...
houses.loc[high_quality & great_views, ['luxury_level']] = 'ultra'
houses["luxury_level"].value_counts()
luxury_level normal 21610 ultra 3 Name: count, dtype: int64