Pandas Basics
| Task | Code | Description |
|---|
| Import pandas | import pandas as pd | Import pandas library |
| Check version | pd.version | Get pandas version |
| Create Series | pd.Series([1,2,3]) | 1D labeled array |
| Create DataFrame | pd.DataFrame({'A':[1,2],'B':[3,4]}) | 2D labeled data |
| Read CSV | pd.read_csv('file.csv') | Load CSV file |
| Read Excel | pd.read_excel('file.xlsx') | Load Excel file |
| Write CSV | df.to_csv('file.csv', index=False) | Save DataFrame as CSV |
| Write Excel | df.to_excel('file.xlsx', index=False) | Save DataFrame as Excel |
Data Inspection
| Task | Code | Description |
|---|
| View top rows | df.head() | First 5 rows |
| View bottom rows | df.tail() | Last 5 rows |
| Shape of DataFrame | df.shape | (rows, columns) |
| Column names | df.columns | Get all column names |
| Index info | df.index | Get row index info |
| Data types | df.dtypes | Data types of columns |
| Summary | df.info() | Summary of DataFrame |
| Statistics | df.describe() | Descriptive stats |
| Memory usage | df.memory_usage() | Memory used by each column |
Selecting Data
| Task | Code | Description |
|---|
| Select column | df['col'] or df.col | Select a single column |
| Select multiple cols | df[['col1','col2']] | Multiple columns |
| Select by position | df.iloc[0] | Row by index |
| Select by label | df.loc[0] | Row by label |
| Slicing rows | df[2:5] | Rows 2–4 |
| Conditional filter | df[df['A'] > 10] | Filter rows |
| Multiple conditions | df[(df['A']>10) & (df['B']<5)] | AND condition |
| isin filter | df[df['A'].isin([1,2,3])] | Match multiple values |
⚙️ Data Cleaning
| Task | Code | Description |
|---|
| Check missing | df.isnull() | Check NaN |
| Drop missing | df.dropna() | Remove NaN rows |
| Fill missing | df.fillna(0) | Replace NaN |
| Drop duplicates | df.drop_duplicates() | Remove duplicate rows |
| Replace values | df.replace('old','new') | Replace specific value |
| Rename columns | df.rename(columns={'old':'new'}) | Rename columns |
| Change type | df['col'] = df['col'].astype(int) | Change data type |
| Reset index | df.reset_index(drop=True) | Reset index |
Data Operations
| Task | Code | Description |
|---|
| Add new column | df['C'] = df['A'] + df['B'] | Add or combine |
| Delete column | df.drop('A', axis=1) | Remove column |
| Apply function | df['A'].apply(lambda x: x*2) | Apply function |
| Sort by column | df.sort_values('A') | Sort by column |
| Sort by index | df.sort_index() | Sort rows by index |
| Unique values | df['A'].unique() | Unique values |
| Value counts | df['A'].value_counts() | Frequency count |
| Rename index | df.rename_axis('index_name') | Rename index |
Grouping & Aggregation
| Task | Code | Description |
|---|
| Group by column | df.groupby('A') | Group data |
| Aggregate | df.groupby('A')['B'].mean() | Mean by group |
| Multiple aggregates | df.groupby('A').agg({'B':['mean','sum']}) | Multiple functions |
| Count per group | df.groupby('A').size() | Count per group |
| Reset group index | .reset_index() | Convert group index to column |
Merging & Joining
| Task | Code | Description |
|---|
| Merge two DFs | pd.merge(df1, df2, on='id') | SQL-style join |
| Left join | pd.merge(df1, df2, how='left', on='id') | Left join |
| Right join | pd.merge(df1, df2, how='right', on='id') | Right join |
| Outer join | pd.merge(df1, df2, how='outer', on='id') | Union join |
| Concatenate | pd.concat([df1, df2]) | Stack DataFrames |
| Join by index | df1.join(df2) | Join on index |
Statistics & Math
| Task | Code | Description |
|---|
| Sum of column | df['A'].sum() | Sum |
| Mean of column | df['A'].mean() | Average |
| Median | df['A'].median() | Median |
| Standard deviation | df['A'].std() | Std. deviation |
| Correlation | df.corr() | Correlation matrix |
| Count non-NA | df.count() | Count of valid values |
Date & Time
| Task | Code | Description |
|---|
| Convert to datetime | pd.to_datetime(df['date']) | Convert string to datetime |
| Extract year | df['date'].dt.year | Year part |
| Extract month | df['date'].dt.month | Month part |
| Extract day | df['date'].dt.day | Day part |
| Filter by date | df[df['date'] > '2024-01-01'] | Filter |
| Date range | pd.date_range('2024-01-01', periods=5) | Create date range |
Input/Output Formats
| Format | Read | Write |
|---|
| CSV | pd.read_csv('file.csv') | df.to_csv('file.csv') |
| Excel | pd.read_excel('file.xlsx') | df.to_excel('file.xlsx') |
| JSON | pd.read_json('file.json') | df.to_json('file.json') |
| HTML | pd.read_html('file.html') | df.to_html('file.html') |
| SQL | pd.read_sql(query, conn) | df.to_sql('table', conn) |
| Pickle | pd.read_pickle('file.pkl') | df.to_pickle('file.pkl') |
Miscellaneous
| Task | Code | Description |
|---|
| Transpose DataFrame | df.T | Flip rows ↔ columns |
| Get index of max | df['A'].idxmax() | Index of max value |
| Get index of min | df['A'].idxmin() | Index of min value |
| Clip values | df['A'].clip(lower=0, upper=10) | Limit values |
| Cumulative sum | df['A'].cumsum() | Running total |
| Apply lambda to DF | df.applymap(lambda x: x*2) | Apply to all elements |