Code
import pandas as pd
Pandas DataFrames
This cheatsheet provides a quick reference for common operations on Pandas DataFrames. Itβs designed for beginning data science students who are just starting to work with Pandas.
Always start by importing pandas:
# Here's an example csv file we can use for read_csv:
from io import StringIO
# Create a CSV string
csv_data = """
Name,Age,City
Alice,25,New York
Bob,30,San Francisco
Charlie,35,Los Angeles
"""
# Use StringIO to create a file-like object
csv_file = StringIO(csv_data.strip())
# Read the CSV data into a DataFrame
df = pd.read_csv(csv_file)
print(df)
Name Age City
0 Alice 25 New York
1 Bob 30 San Francisco
2 Charlie 35 Los Angeles
Name Age City
0 Alice 25 New York
1 Bob 30 San Francisco
2 Charlie 35 Los Angeles
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Name 3 non-null object
1 Age 3 non-null int64
2 City 3 non-null object
dtypes: int64(1), object(2)
memory usage: 204.0+ bytes
None
Age
count 3.0
mean 30.0
std 5.0
min 25.0
25% 27.5
50% 30.0
75% 32.5
max 35.0
Index(['Name', 'Age', 'City'], dtype='object')
(3, 3)
For more advanced operations and in-depth explanations, check out these resources:
Remember, practice is key! Try these operations with different datasets to become more comfortable with Pandas DataFrames.