Pandas Cheat Sheet
Python Pandas library reference guide
Pandas Cheat Sheet
Quick reference guide for Pandas DataFrame operations. Click the copy button to copy any command.
93
Total Commands
93
Filtered Results
Importing
Command | Copy | Description |
---|---|---|
import pandas as pd | Import pandas library | |
import numpy as np | Import numpy (often used with pandas) |
Reading Data
Command | Copy | Description |
---|---|---|
pd.read_csv("file.csv") | Read CSV file | |
pd.read_excel("file.xlsx") | Read Excel file | |
pd.read_json("file.json") | Read JSON file | |
pd.read_sql(query, connection) | Read from SQL database | |
pd.read_html(url) | Read HTML tables |
Writing Data
Command | Copy | Description |
---|---|---|
df.to_csv("file.csv") | Write to CSV file | |
df.to_excel("file.xlsx") | Write to Excel file | |
df.to_json("file.json") | Write to JSON file | |
df.to_sql("table", connection) | Write to SQL database |
Creating DataFrames
Command | Copy | Description |
---|---|---|
pd.DataFrame(data) | Create DataFrame from dict/list | |
pd.DataFrame(data, columns=["A", "B"]) | Create DataFrame with column names | |
pd.Series([1, 2, 3]) | Create Series |
Viewing Data
Command | Copy | Description |
---|---|---|
df.head() | View first 5 rows | |
df.tail() | View last 5 rows | |
df.sample(5) | View random 5 rows | |
df.info() | Get DataFrame info | |
df.describe() | Get statistical summary | |
df.shape | Get dimensions (rows, columns) | |
df.columns | Get column names | |
df.dtypes | Get data types | |
df.index | Get index |
Selection
Command | Copy | Description |
---|---|---|
df["column"] | Select single column | |
df[["col1", "col2"]] | Select multiple columns | |
df.loc[row_label] | Select by label | |
df.iloc[row_index] | Select by position | |
df.loc[0:5, "column"] | Select rows and column by label | |
df.iloc[0:5, 0:2] | Select rows and columns by position |
Filtering
Command | Copy | Description |
---|---|---|
df[df["col"] > 5] | Filter rows by condition | |
df[(df["col1"] > 5) & (df["col2"] < 10)] | Filter with multiple conditions (AND) | |
df[(df["col1"] > 5) | (df["col2"] < 10)] | Filter with multiple conditions (OR) | |
df[df["col"].isin([1, 2, 3])] | Filter by values in list | |
df[df["col"].str.contains("text")] | Filter by string contains |
Sorting
Command | Copy | Description |
---|---|---|
df.sort_values("column") | Sort by column (ascending) | |
df.sort_values("column", ascending=False) | Sort descending | |
df.sort_values(["col1", "col2"]) | Sort by multiple columns | |
df.sort_index() | Sort by index |
Adding/Removing Columns
Command | Copy | Description |
---|---|---|
df["new_col"] = values | Add new column | |
df.drop("column", axis=1) | Drop column | |
df.drop(["col1", "col2"], axis=1) | Drop multiple columns | |
df.rename(columns={"old": "new"}) | Rename column |
Adding/Removing Rows
Command | Copy | Description |
---|---|---|
df.drop(index) | Drop row by index | |
pd.concat([df1, df2]) | Concatenate DataFrames | |
df.append(other_df) | Append rows |
Handling Missing Data
Command | Copy | Description |
---|---|---|
df.isna() | Check for missing values | |
df.isnull() | Check for null values | |
df.dropna() | Drop rows with missing values | |
df.fillna(value) | Fill missing values | |
df.fillna(method="ffill") | Forward fill missing values | |
df.fillna(method="bfill") | Backward fill missing values |
GroupBy
Command | Copy | Description |
---|---|---|
df.groupby("column").mean() | Group by and calculate mean | |
df.groupby("column").sum() | Group by and calculate sum | |
df.groupby("column").count() | Group by and count | |
df.groupby(["col1", "col2"]).mean() | Group by multiple columns | |
df.groupby("column").agg(["mean", "sum"]) | Multiple aggregations |
Merge/Join
Command | Copy | Description |
---|---|---|
pd.merge(df1, df2, on="key") | Merge on column | |
pd.merge(df1, df2, left_on="key1", right_on="key2") | Merge on different columns | |
pd.merge(df1, df2, how="left") | Left join | |
pd.merge(df1, df2, how="right") | Right join | |
pd.merge(df1, df2, how="outer") | Outer join | |
pd.merge(df1, df2, how="inner") | Inner join |
Apply Functions
Command | Copy | Description |
---|---|---|
df["col"].apply(func) | Apply function to column | |
df.apply(func) | Apply function to DataFrame | |
df["col"].map(dict) | Map values using dictionary | |
df.applymap(func) | Apply function to each element |
String Operations
Command | Copy | Description |
---|---|---|
df["col"].str.lower() | Convert to lowercase | |
df["col"].str.upper() | Convert to uppercase | |
df["col"].str.strip() | Remove whitespace | |
df["col"].str.replace("old", "new") | Replace string | |
df["col"].str.split(",") | Split string | |
df["col"].str.contains("text") | Check if contains text |
Date/Time
Command | Copy | Description |
---|---|---|
pd.to_datetime(df["col"]) | Convert to datetime | |
df["date"].dt.year | Extract year | |
df["date"].dt.month | Extract month | |
df["date"].dt.day | Extract day | |
df["date"].dt.dayofweek | Get day of week |
Statistics
Command | Copy | Description |
---|---|---|
df["col"].mean() | Calculate mean | |
df["col"].median() | Calculate median | |
df["col"].std() | Calculate standard deviation | |
df["col"].var() | Calculate variance | |
df["col"].min() | Get minimum value | |
df["col"].max() | Get maximum value | |
df["col"].sum() | Calculate sum | |
df["col"].count() | Count non-null values | |
df["col"].value_counts() | Count unique values | |
df["col"].unique() | Get unique values | |
df["col"].nunique() | Count unique values |
Pivot Tables
Command | Copy | Description |
---|---|---|
df.pivot_table(values="val", index="idx", columns="col") | Create pivot table | |
pd.crosstab(df["col1"], df["col2"]) | Create cross-tabulation |
Reset Index
Command | Copy | Description |
---|---|---|
df.reset_index() | Reset index to default | |
df.reset_index(drop=True) | Reset index and drop old | |
df.set_index("column") | Set column as index |
All operations are performed locally in your browser. No data is sent to any server.
About Pandas Cheat Sheet
This section will contain detailed, SEO-friendly content about the Pandas Cheat Sheet.
In the future, this content will be managed through a headless CMS, allowing you to:
- Add detailed explanations about how to use this tool
- Include examples and use cases
- Provide tips and best practices
- Add FAQs and troubleshooting guides
- Update content without touching the code
How to Use
Step-by-step instructions for using the Pandas Cheat Sheet will appear here. This content will be fully customizable through the admin panel.
Features
Key features and benefits of this tool will be listed here. All content is editable via the CMS.