The following list below are solutions for the pandas exercises given in the pandas python lesson at 4Geeks.com, click here to access the exercise instructions.
import numpy as np
import pandas as pd
np.random.seed(42)
# From list
l = [1, 2, 3, 4, 5, 6]
serie = pd.Series(l)
print(serie)
# From NumPy array
array = np.array([1, 2, 3, 4, 5, 6])
serie = pd.Series(array)
print(serie)
# From dictionary
d = {"A": 1, "B": 2, "C": 3}
serie = pd.Series(d)
print(serie)
# From NumPy array
array = np.random.randint(1, 10, size = (5, 5))
dataframe = pd.DataFrame(array)
dataframe
# From dictionary
d = {
"A": np.random.randint(10, 100, size = 5),
"B": np.linspace(1, 10, 5),
"C": np.random.randn(5)
}
dataframe = pd.DataFrame(d)
dataframe
# From list of tuples
t = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
dataframe = pd.DataFrame(t)
dataframe
s1 = pd.Series([1, 2, 3, 4, 5])
s2 = pd.Series([4, 5, 6, 7, 8])
# Method 1
dataframe = pd.DataFrame({"ser1": s1, "ser2": s2})
dataframe = pd.DataFrame({"ser1": s1, "ser2": s2}, index = s1.index)
dataframe
# Method 2
dataframe = pd.concat([s1, s2], axis = 1)
dataframe
# Method 3
s1.name = "ser1"
s2.name = "ser2"
dataframe = s1.to_frame().join(s2)
dataframe
s1 = pd.Series([1, 2, 3, 4, 5])
s2 = pd.Series([4, 5, 6, 7, 8])
# Method 1: Using Pandas function
filtering_results = s1.isin(s2)
indices = s1[filtering_results].index
indices
# Method 2: Using NumPy function
indices = np.where(s1.isin(s2))
indices
# Method 3: Using Python
indices = []
for value in s1.values:
if value in s2.values:
indices.append(s1[s1 == value].index[0])
indices
s1 = pd.Series([1, 2, 3, 4, 5])
s2 = pd.Series([4, 5, 6, 7, 8])
# Method 1
unique_s1 = s1[~s1.isin(s2)]
unique_s2 = s2[~s2.isin(s1)]
unique_elements = np.concatenate([unique_s1, unique_s2])
unique_elements
# Method 2
concat = pd.concat([s1, s2])
unique_elements = concat[~concat.duplicated(keep = False)].values
unique_elements
df = pd.DataFrame(np.random.rand(10, 5) * 10, columns = [f"Col {i}" for i in range(5)])
df
df.sort_values("Col 0")
df.sort_values(by = ["Col 2", "Col 4"])
df.columns = [f"{i}_column" for i in range(5)]
df
df.index = [f"{i}_row" for i in range(10)]
df