This example demonstrates filtering, sorting, and string manipulation using a customer DataFrame in pandas.
import pandas as pd
# Create the DataFrame
data = {
'CustomerID': [201, 202, 203, 204, 205, 206, 207, 208, 209, 210],
'Name': ['Raj', 'Simran', 'Amit', 'Priya', 'Karan', 'Neha', 'Anil', 'Meera', 'Ravi', 'Sana'],
'Age': [34, 28, 45, 32, 38, 26, 41, 30, 36, 29],
'City': ['Delhi', 'Mumbai', 'Chennai', 'Kolkata', 'Delhi', 'Bangalore', 'Mumbai', 'Hyderabad', 'Jaipur', 'Pune'],
'TotalPurchase': [15000, 25000, 18000, 22000, 17000, 30000, 16000, 20000, 24000, 19000]
}
df = pd.DataFrame(data)
print("\nCustomers with purchases > 20000:")
print(df[df['TotalPurchase'] > 20000])
print("\nCustomers from Delhi or Mumbai and age < 35:")
print(df[(df['City'].isin(['Delhi', 'Mumbai'])) & (df['Age'] < 35)])
print("\nCustomers whose names end with 'a' and purchases > 18000:")
print(df[df['Name'].str.endswith('a') & (df['TotalPurchase'] > 18000)])
print("\nCustomers whose names contain the letter 'i' (case insensitive):")
print(df[df['Name'].str.contains('i', case=False)])
print("\nDataFrame sorted by TotalPurchase (ascending):")
print(df.sort_values(by='TotalPurchase'))
print("\nDataFrame sorted by Age (descending):")
print(df.sort_values(by='Age', ascending=False))
print("\nDataFrame sorted by City and then TotalPurchase (descending):")
print(df.sort_values(by=['City', 'TotalPurchase'], ascending=[True, False]))
print("\nDataFrame sorted by index (reverse):")
print(df.sort_index(ascending=False))
# Convert names to uppercase and create CustomerCode
df['Name'] = df['Name'].str.upper()
df['CustomerCode'] = df['Name'].str[:3] + df['CustomerID'].astype(str)
print("\nAll names converted to uppercase and CustomerCode column added:")
print(df[['CustomerID', 'Name', 'CustomerCode']])
print("\nCustomers with names longer than 4 characters:")
print(df[df['Name'].str.len() > 4])
print("\nNames with 'a' replaced by '@':")
df['Name'] = df['Name'].str.replace('A', '@', case=False, regex=False)
print(df[['CustomerID', 'Name']])
df['TotalPurchase'] > 20000 filters customers by purchase amount.df['City'].isin(['Delhi', 'Mumbai']) filters by multiple cities.str.endswith() and str.contains() are used for string-based filters.sort_values().sort_index().str.upper().CustomerCode.str.replace().