Purpose: To remove specified labels from rows or columns.
Syntax: drop(index=None, labels=None, axis=0, inplace=False)
index
: Specify the row indices to drop.labels
: Specify column labels to drop.axis
: 0 for rows, 1 for columns.inplace
: If True, drop is done in place and the method returns None
.Example Usage:
result = country.drop(columns=['Percentage'])
print(result)
Purpose: To remove duplicate rows based on specific criteria.
Syntax: drop_duplicates(subset=None, inplace=False)
Example Usage:
result = country.drop_duplicates(subset=['Continent'])
print(result)
result = country.dropna(axis=0)
print(result)
result = country.dropna(axis=1)
print(result)
result = country.sort_values(by='Area')
print(result)
result = country.sort_values(by='Area', ascending=False)
DataFrame.merge(leftdataframe, rightdataframe, how='outer', on=None)
how
parameter defines the type of join (e.g., INNER JOIN, LEFT JOIN). result = pd.merge(student, registration, how='inner', left_on='Panther ID', right_on='Student ID')
result = pd.merge(student, registration, how='left', left_on='Panther ID', right_on='Student ID')
result = pd.merge(student, registration, how='right', left_on='Panther ID', right_on='Student ID')
result = pd.merge(student, registration, how='outer', left_on='Panther ID', right_on='Student ID')
student
: student = pd.DataFrame([[1,'Michael'],[2,'Henry'],[3,'Sarah'],[4,'John']], columns=['Panther ID','Name'])
registration
: registration = pd.DataFrame([[1,'CSC1301', 2],[2,'CSC1302', 1],[3,'CSC2720', 1],[4,'CSC4120', 2], [5,'CSC4330',5]], columns=['Reg ID','Course', 'Student ID'])
Exercise 1: Merge two DataFrames using an inner join based on member_id and member columns.
Example Data:
members
:
member_id | name | |
---|---|---|
1 | A | a@gmail.com |
2 | B | b@outlook.com |
3 | C | c@yahoo.com |
orders
:
order_id | product | price | member |
---|---|---|---|
1000 | Laptop | 800 | 2 |
1001 | Phone | 700 | 1 |
Merge with:
result = pd.merge(members, orders, how='inner', left_on='member_id', right_on='member')