0% found this document useful (0 votes)
20 views2 pages

Pandas Cheat Sheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Pandas Cheat Sheet

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Pandas Cheat Sheet

1. Creating DataFrames and Series

import pandas as pd

# From dictionary
data = {'Name': ['Alice', ' 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)

# From list (Series)


s = pd.Series([1, 2, 3])

2. Reading and Writing Data

df = pd.read_csv('file.csv') # Read CSV


df.to_csv('output.csv') # Write CSV

3. Exploring Data

df.head() # First 5 rows


df.tail() # Last 5 rows
df.shape # Dimensions
df.info() # Summary info
df.describe() # Descriptive stats

4. Selecting Data

df['column'] # Single column


df[['col1', 'col2']] # Multiple columns
df.loc[0] # Row by label
df.iloc[0] # Row by index
df[df['Age'] > 25] # Conditional filtering

5. Modifying Data

df['Age'] = df['Age'] + 1 # Modify column


df.rename(columns={'Name': 'Full Name'}, inplace=True)
df.drop('column', axis=1) # Drop column
df.drop(0, axis=0) # Drop row

6. Handling Missing Data


Pandas Cheat Sheet

df.isnull() # Detect missing


df.dropna() # Drop missing
df.fillna(0) # Replace with 0

7. Grouping and Aggregation

df.groupby('Category').sum()
df.groupby('Type')['Sales'].mean()

8. Merging and Joining

pd.merge(df1, df2, on='id') # Inner join


pd.concat([df1, df2]) # Concatenate

9. Sorting and Duplicates

df.sort_values('Age') # Sort by column


df.duplicated() # Detect duplicates
df.drop_duplicates() # Remove duplicates

10. Useful Functions

df['col'].unique() # Unique values


df['col'].value_counts() # Frequency counts
df.apply(len) # Apply function

You might also like