Table of Contents
Python has become one of the most popular programming languages for automation and data analysis. Its simplicity and versatility make it ideal for streamlining complex tasks and extracting insights from large datasets. In this article, we present 10 ready-to-use prompts that can help students and professionals enhance their productivity and analytical capabilities with Python.
1. Automate File Renaming
Use this prompt to batch rename files in a directory based on specific patterns or metadata. It saves time when organizing large collections of files.
import os
directory = 'path/to/your/files'
for filename in os.listdir(directory):
new_name = filename.replace('old_pattern', 'new_pattern')
os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))
2. Data Cleaning in Pandas
This prompt helps clean missing data, duplicate entries, and inconsistent formatting in datasets using pandas.
import pandas as pd
df = pd.read_csv('your_dataset.csv')
df.drop_duplicates(inplace=True)
df.fillna(method='ffill', inplace=True)
df['column_name'] = df['column_name'].str.strip()
df.to_csv('cleaned_dataset.csv', index=False)
3. Automate Web Scraping
Extract data from websites automatically using BeautifulSoup and requests libraries.
Note: Always respect website terms of service and robots.txt files.
import requests
from bs4 import BeautifulSoup
url = 'https://example.com/data-page'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find_all('div', class_='target-class')
for item in data:
print(item.text.strip())
4. Generate Summary Statistics
Quickly obtain descriptive statistics for numerical columns in a dataset.
import pandas as pd
df = pd.read_csv('your_dataset.csv')
print(df.describe())
5. Plot Data Visualizations
Create visualizations like histograms, scatter plots, and line charts using Matplotlib or Seaborn.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
df = pd.read_csv('your_dataset.csv')
sns.histplot(df['numeric_column'])
plt.show()
6. Automate Email Sending
Send automated emails with attachments or reports using smtplib.
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg['Subject'] = 'Automated Report'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg.set_content('Please find the attached report.')
with open('report.pdf', 'rb') as f:
file_data = f.read()
msg.add_attachment(file_data, maintype='application', subtype='pdf', filename='report.pdf')
with smtplib.SMTP('smtp.example.com', 587) as server:
server.login('[email protected]', 'password')
server.send_message(msg)
7. Automate Data Entry
Use Python to input data into forms or spreadsheets automatically.
import openpyxl
wb = openpyxl.load_workbook('data.xlsx')
ws = wb.active
ws['A1'] = 'Sample Data'
wb.save('data_updated.xlsx')
8. Machine Learning Model Training
Train simple machine learning models with scikit-learn for classification or regression tasks.
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2)
model = RandomForestClassifier()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print('Accuracy:', accuracy_score(y_test, predictions))
9. Automate Backup of Files
Schedule backups of important files or directories using Python scripts.
import shutil
import datetime
import os
source_dir = 'path/to/important/files'
backup_dir = 'path/to/backup/location'
backup_name = f"backup_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}"
shutil.copytree(source_dir, os.path.join(backup_dir, backup_name))
10. Automate Task Scheduling
Combine Python scripts with scheduling libraries like schedule or cron to run tasks automatically at specified times.
import schedule
import time
def job():
print("Running scheduled task...")
schedule.every().day.at("10:00").do(job)
while True:
schedule.run_pending()
time.sleep(60)
These prompts provide a solid foundation for automating routine tasks and conducting data analysis efficiently with Python. Customize and expand them according to your specific needs to maximize productivity and insights.