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¶
countriesLoading...
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¶
titanicLoading...
titanic["sex"] = titanic["sex"].replace("female", "F")
titanicLoading...
titanic["sex"] = titanic["sex"].replace("F", "female")
titanicLoading...
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: objecttitanic["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: objecttitanicLoading...
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: int64titanic["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: int64titanic["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: int64titanic["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: int64Updating With .loc[ ]¶
countriesLoading...
countries.loc["Finland", ["Ladder score", "Healthy life expectancy"]]Ladder score 7.842
Healthy life expectancy 72.0
Name: Finland, dtype: objectcountries.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"countriesLoading...
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"] >= 10houses.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...
housesLoading...
high_quality = houses["grade"] > 12
great_views = houses["view"] == 4houses['luxury_level'] = "normal"housesLoading...
houses.loc[high_quality & great_views, ['luxury_level']] = 'ultra'houses["luxury_level"].value_counts()luxury_level
normal 21610
ultra 3
Name: count, dtype: int64