20 Essential Python Code Snippets for Data Scientists
Upgrade your Python skills
3 min readSep 3, 2023
Python is the go-to language for data scientists, thanks to its versatility and rich ecosystem of libraries. In this article, we’ll explore 20 important Python code snippets every data scientist should have in their toolkit. These snippets cover a wide range of data manipulation and analysis tasks.
1. Importing Libraries:
Always start by importing the necessary libraries for your project.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
2. Reading Data:
Load data from various sources, such as CSV, Excel, or SQL databases.
# From CSV
data = pd.read_csv('data.csv')
# From Excel
data = pd.read_excel('data.xlsx')
# From SQL
import sqlite3
conn = sqlite3.connect('database.db')
data = pd.read_sql_query('SELECT * FROM table_name', conn)
3. Data Inspection:
Quickly check the first few rows and basic statistics of your data.
data.head()
data.describe()