Pandas Cheat Sheet
Pandas is a powerful data manipulation library for Python, and having a handy cheat sheet can be immensely useful for quick reference. This cheat sheet provides a concise overview of common Pandas operations, syntax, and tips.
Syntax
import pandas as pd
# Reading data from a CSV file
df = pd.read_csv('filename.csv')
# Selecting columns
df['column_name']
df[['column1', 'column2']]
# Filtering rows
df[df['column'] > 50]
# Adding a new column
df['new_column'] = df['column1'] + df['column2']
# Grouping data
df.groupby('column').mean()
# Merging DataFrames
pd.merge(df1, df2, on='common_column', how='inner')
# Handling missing data
df.dropna()
df.fillna(value)
# Pivot table
pd.pivot_table(df, values='value', index='index_column', columns='column_to_pivot')
# Basic statistics
df.describe()
# Plotting
df['column'].plot(kind='bar')
Example
import pandas as pd
import matplotlib.pyplot as plt
# Creating a DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'San Francisco', 'Los Angeles']}
df = pd.DataFrame(data)
# Displaying the DataFrame
print(df)
# Plotting the 'Age' column
df['Age'].plot(kind='bar')
plt.xlabel('Index')
plt.ylabel('Age')
plt.title('Age Distribution')
plt.show()
Output
Name Age City
0 Alice 25 New York
1 Bob 30 San Francisco
2 Charlie 35 Los Angeles
Explanation
- The cheat sheet covers essential Pandas operations, including reading data, selecting columns, filtering rows, adding new columns, grouping data, merging DataFrames, handling missing data, creating pivot tables, basic statistics, and plotting.
Use
- Use this cheat sheet as a quick reference for common Pandas tasks during data analysis and manipulation.
- Customize the provided examples based on your specific use case and data.
Important Points
- Pandas offers a wide range of functionalities; the cheat sheet includes commonly used operations.
- Explore the official Pandas documentation for more in-depth details.
Summary
The Pandas cheat sheet serves as a handy guide for quickly accessing common Pandas operations. Keep it within reach as you work with Pandas, and customize the examples based on your specific data analysis needs. The combination of syntax, examples, and important points makes this cheat sheet a valuable resource for both beginners and experienced users.