Manipulating DataFrame Columns
What are the main operations on a DataFrame’s columns?
How do I rename or drop a column in Pandas?
# import pandas with the socially accepted alias 'pd'
>>> import pandas as pd
# the df
>>> df = pd.DataFrame(
{
"laptop": ["MacBook Pro 13inch", "Thinkpad T14", "Dell XPS 13"],
"ram": ["16 GB", "48 GB", "8 GB"],
"os": ["macOS Monterey", "Debian 11", "Windows 11"],
"chip": ["M1", "Ryzen", "Intel Core i7"]
}
)
# data view
>>> df
laptop ram os chip
0 MacBook Pro 13inch 16 GB macOS Monterey M1
1 Thinkpad T14 48 GB Debian 11 Ryzen
2 Dell XPS 13 8 GB Windows 11 Intel Core i7
# info
>>> df.info()
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
Column Non-Null Count Dtype
--- ------ -------------- -----
0 laptop 3 non-null str
1 ram 3 non-null str
2 os 3 non-null str
3 chip 3 non-null str
dtypes: str(4)
memory usage: 228.0 bytes
# rename the column "ram" to "memory"
>>> df.rename(columns={"ram": "memory"}, inplace=True)
>>> df
laptop memory os chip
0 MacBook Pro 13inch 16 GB macOS Monterey M1
1 Thinkpad T14 48 GB Debian 11 Ryzen
2 Dell XPS 13 8 GB Windows 11 Intel Core i7
# drop the column "chip"
# --+ when inplace is set to False, the outcome of .drop is sent to the
# interactive sessions, but the data in memory are not affected
>>> df.drop(columns=["chip"], inplace=False)
laptop memory os
0 MacBook Pro 13inch 16 GB macOS Monterey
1 Thinkpad T14 48 GB Debian 11
2 Dell XPS 13 8 GB Windows 11
# --+ when inplace is set to True, the data in memory are affected
>>> df.drop(columns=["chip"], inplace=True)
>>> df
laptop memory os
0 MacBook Pro 13inch 16 GB macOS Monterey
1 Thinkpad T14 48 GB Debian 11
2 Dell XPS 13 8 GB Windows 11How do I create a new column or amend an existing Pandas column?
# import pandas with the socially accepted alias 'pd'
>>> import pandas as pd
# the df
>>> df = pd.DataFrame(
{
"laptop": ["MacBook Pro 13inch", "Thinkpad T14", "Dell XPS 13"],
"ram": ["16 GB", "48 GB", "8 GB"],
"os": ["macOS Monterey", "Debian 11", "Windows 11"],
"chip": ["M1", "Ryzen", "Intel Core i7"],
}
)
# create a new column, e.g., the name of the manufacturer
>>> df.loc[:, "manufacturer"] = ["Apple", "Lenovo", "Dell"]
>>> df
laptop ram os chip manufacturer
0 MacBook Pro 13inch 16 GB macOS Monterey M1 Apple
1 Thinkpad T14 48 GB Debian 11 Ryzen Lenovo
2 Dell XPS 13 8 GB Windows 11 Intel Core i7 Dell
# transform the column "ram" from string to number type
# --+ step 1: get rid of non-number characters using a regular expression
# NOTE two things here: plain [] assignment, not .loc (see below), and
# regex=True, which since Pandas 2.0 you must ask for explicitly
>>> df["ram"] = df["ram"].str.replace(r"[^0-9]", "", regex=True)
# --+ alternative way to carry out step 1: using Pandas string methods
>>> df["ram"] = df["ram"].str.replace(r"\sGB", "", regex=True)
# --+ step 2: convert the string to number type
# ----+ check the type of "ram"
>>> df.info()
Data columns (total 5 columns):
Column Non-Null Count Dtype
--- ------ -------------- -----
0 laptop 3 non-null str
1 ram 3 non-null str
2 os 3 non-null str
3 chip 3 non-null str
4 manufacturer 3 non-null str
dtypes: str(5)
# ----+ surprise-surprise: we have not a number type yet
# let us change dtype with Pandas astype method
# again [] and not .loc --- .loc writes INTO the existing text column,
# and on Pandas 3 that is a TypeError:
# "Invalid value '[16 48 8]' for dtype 'str'"
>>> df["ram"] = df["ram"].astype(int)
# ----+ check the data type again
>>> df.info()
Data columns (total 5 columns):
Column Non-Null Count Dtype
--- ------ -------------- -----
0 laptop 3 non-null str
1 ram 3 non-null int64
2 os 3 non-null str
3 chip 3 non-null str
4 manufacturer 3 non-null str
dtypes: int64(1), str(4)
memory usage: 252.0 bytes
# transform an existing column and assign the output to a new column
# --+ import numpy to access the log function
>>> import numpy as np
>>> df["log_ram"] = np.log(df["ram"])
# --+ preview
>>> df
laptop ram os chip manufacturer \
0 MacBook Pro 13inch 16 macOS Monterey M1 Apple
1 Thinkpad T14 48 Debian 11 Ryzen Lenovo
2 Dell XPS 13 8 Windows 11 Intel Core i7 Dell
log_ram
0 2.772589
1 3.871201
2 2.079442
# --+ info
>>> df.info()
Data columns (total 6 columns):
Column Non-Null Count Dtype
--- ------ -------------- -----
0 laptop 3 non-null str
1 ram 3 non-null int64
2 os 3 non-null str
3 chip 3 non-null str
4 manufacturer 3 non-null str
5 log_ram 3 non-null float64
dtypes: float64(1), int64(1), str(4)
memory usage: 276.0 bytes
# create a new column conditional on another column's value
>>> df.loc[df["laptop"] == "Thinkpad T14", "gpu"] = True
# --+ info
>>> df.info()
Data columns (total 7 columns):
Column Non-Null Count Dtype
--- ------ -------------- -----
0 laptop 3 non-null str
1 ram 3 non-null int64
2 os 3 non-null str
3 chip 3 non-null str
4 manufacturer 3 non-null str
5 log_ram 3 non-null float64
6 gpu 1 non-null object
dtypes: float64(1), int64(1), object(1), str(4)
memory usage: 300.0+ bytes
# --+ preview
>>> df
laptop ram os chip manufacturer \
0 MacBook Pro 13inch 16 macOS Monterey M1 Apple
1 Thinkpad T14 48 Debian 11 Ryzen Lenovo
2 Dell XPS 13 8 Windows 11 Intel Core i7 Dell
log_ram gpu
0 2.772589 NaN
1 3.871201 True
2 2.079442 NaNWhat is a missing value?
How does Pandas represent missing values?
How do I handle missing values in Pandas?
# import numpy with the socially accepted alias "np"
>>> import numpy as np
# import pandas with the socially acceptable alias "pd"
>>> import pandas as pd
# the dataframe
>>> df = pd.DataFrame(
{
"item": ["a", "b", "c", "d", "e"],
"price": [16.32, 16.78, np.nan, np.nan, 16.41],
}
)
# data view
>>> df
item price
0 a 16.32
1 b 16.78
2 c NaN
3 d NaN
4 e 16.41
# info
>>> df.info()
Data columns (total 2 columns):
Column Non-Null Count Dtype
--- ------ -------------- -----
0 item 5 non-null str
1 price 3 non-null float64
dtypes: float64(1), str(1)
memory usage: 212.0 bytes
# approach 1 --- delete all cases where a missing value is present
>>> df.dropna()
item price
0 a 16.32
1 b 16.78
4 e 16.41
# approach 2 --- replace missing values with a fixed value
>>> df.fillna(value=16.99, inplace=False)
item price
0 a 16.32
1 b 16.78
2 c 16.99
3 d 16.99
4 e 16.41
# approach 3 --- replace missing values with the mean of the columns
>>> df.fillna(value=np.mean(df["price"]), inplace=False)
item price
0 a 16.320000
1 b 16.780000
2 c 16.503333
3 d 16.503333
4 e 16.410000