Make a ipynb file with all the basic functions of pandas explaining them too
- User
Make a ipynb file with all the basic functions of pandas explaining them too
- Assistant
- User
- Assistant
- User
"execution_count": null, this giving error
- Assistant
Make a ipynb file with all the basic functions of pandas explaining them too
"execution_count": null, this giving error
To create an .ipynb file (Jupyter Notebook) that includes explanations and demonstrations of basic functions of the Pandas library, follow the outline given below. The notebook will cover the following topics:
Here's how you can structure and create the notebook:
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to Pandas\n",
"\n",
"Pandas is a fast, powerful, flexible, and easy-to-use open-source data analysis and data manipulation library built on top of the Python programming language."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# First, let's import the pandas library\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating DataFrames and Series\n",
"\n",
"Pandas primarily uses two data structures: `Series` and `DataFrame`. A `Series` is a one-dimensional labeled array, while a `DataFrame` is a two-dimensional labeled data structure with columns that potentially hold different types.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Create a Series\n",
"ser = pd.Series([1, 2, 3, 4, 5])\n",
"print(\"Series:\\n\", ser)\n",
"\n",
"# Create a DataFrame\n",
"data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}\n",
"df = pd.DataFrame(data)\n",
"print(\"\\nDataFrame:\\n\", df)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic DataFrame Operations\n",
"\n",
"These operations are fundamental for data manipulation."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Basic inspection of DataFrame\n",
"print(df.head()) # First 5 rows\n",
"print(df.tail()) # Last 5 rows\n",
"print(df.info()) # Column information\n",
"print(df.describe()) # Descriptive statistics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Selection and Filtering\n",
"\n",
"You can select rows and columns using label-based indexing (`.loc`) and integer-based indexing (`.iloc`)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Selecting by label\n",
"print(df.loc[0, 'A']) # First row, 'A' column\n",
"\n",
"# Selecting by position\n",
"print(df.iloc[0]) # First row\n",
"print(df.iloc[:, 0]) # First column"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Cleaning\n",
"\n",
"Data cleaning involves handling missing data, duplicates, and data transformation."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Handling missing data\n",
"df_with_nan = df.copy()\n",
"df_with_nan.loc[1, 'A'] = None\n",
"print(df_with_nan.fillna(0)) # Fill missing with 0\n",
"\n",
"# Dropping duplicates\n",
"df_with_duplicates = df.append(df.iloc[0], ignore_index=True)\n",
"print(df_with_duplicates.drop_duplicates())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Merging and Joining\n",
"\n",
"Pandas provides functions for combining DataFrames using a variety of joins, typical of SQL-like operations."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Concatenate DataFrames\n",
"df2 = pd.DataFrame({'A': [10, 11], 'B': [12, 13], 'C': [14, 15]})\n",
"df_concatenated = pd.concat([df, df2], ignore_index=True)\n",
"print(df_concatenated)\n",
"\n",
"# Merge or join\n",
"left = pd.DataFrame({'key': ['K0', 'K1'], 'A': ['A0', 'A1']})\n",
"right = pd.DataFrame({'key': ['K0', 'K2'], 'B': ['B0', 'B2']})\n",
"merged = pd.merge(left, right, on='key', how='left')\n",
"print(merged)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Grouping and Aggregation\n",
"\n",
"These functions allow you to perform splits (grouping data into groups), apply a function, and combine the result."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Group by and aggregate\n",
"df_for_grouping = pd.DataFrame({\n",
" 'Category': ['A', 'B', 'A', 'B'],\n",
" 'Values': [10, 20, 15, 25]\n",
"})\n",
"grouped = df_for_grouping.groupby('Category').sum()\n",
"print(grouped)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Input/Output\n",
"\n",
"Pandas can read/write data from various file formats: CSV, Excel, SQL, and more."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Reading and writing CSV\n",
"# df.to_csv('out.csv') # Write to CSV\n",
"# df_read = pd.read_csv('out.csv') # Read from CSV\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This concludes the basic functionality of the Pandas library. Experiment with these functions and explore the official documentation for more advanced use cases."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
.ipynb, such as Basic_Pandas_Tutorial.ipynb.This notebook includes a brief introduction to basic functions and operations in pandas. It is advisable to complement this with more detailed documentation depending on your learning or project needs.
{
"cells": [
{
"cell_type": "markdown",
"id": "c922f2ad",
"metadata": {},
"source": [
"# Basic Functions of Pandas"
]
},
{
"cell_type": "markdown",
"id": "162663b9",
"metadata": {},
"source": [
"### 1. Importing Pandas\n",
"To work with pandas, you first need to import it."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37aec462",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "e9ebc191",
"metadata": {},
"source": [
"### 2. Creating a DataFrame\n",
"The DataFrame is one of the most commonly used objects in pandas. It's a 2-dimensional labeled data structure."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4231e02",
"metadata": {},
"outputs": [],
"source": [
"data = {\n",
" 'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],\n",
" 'Age': [24, 27, 22, 32, 29],\n",
" 'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],\n",
" 'Salary': [50000, 60000, 55000, 62000, 58000]\n",
"}\n",
"\n",
"df = pd.DataFrame(data)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "af5ff40d",
"metadata": {},
"source": [
"### 3. Reading Data\n",
"You can read data from files like CSVs, Excel, SQL databases, etc."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4cef1cbc",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('sample_data.csv')\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "72082be0",
"metadata": {},
"source": [
"### 4. Basic Data Exploration\n",
"Use functions like head(), info(), describe() to quickly inspect your data."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f3b1f9f",
"metadata": {},
"outputs": [],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd75461e",
"metadata": {},
"outputs": [],
"source": [
"df.info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c560de9b",
"metadata": {},
"outputs": [],
"source": [
"df.describe()"
]
},
{
"cell_type": "markdown",
"id": "bde1a93c",
"metadata": {},
"source": [
"### 5. Selecting Data\n",
"You can select data using labels (loc) or by index positions (iloc)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e9ca66d",
"metadata": {},
"outputs": [],
"source": [
"# Select by label\n",
"df.loc[0, 'Name']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7f8be0f",
"metadata": {},
"outputs": [],
"source": [
"# Select by index position\n",
"df.iloc[0, 0]"
]
},
{
"cell_type": "markdown",
"id": "c142ba87",
"metadata": {},
"source": [
"### 6. Filtering Data\n",
"You can filter data based on conditions."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91b8d523",
"metadata": {},
"outputs": [],
"source": [
"# Filter rows where age is greater than 25\n",
"df[df['Age'] > 25]"
]
},
{
"cell_type": "markdown",
"id": "65343816",
"metadata": {},
"source": [
"### 7. Adding a New Column\n",
"You can add new columns to the DataFrame."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65f6a0e8",
"metadata": {},
"outputs": [],
"source": [
"# Adding a new column 'Tax'\n",
"df['Tax'] = df['Salary'] * 0.1\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "d885c5cc",
"metadata": {},
"source": [
"### 8. Dropping Columns or Rows\n",
"Use drop() to remove rows or columns from the DataFrame."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd52007d",
"metadata": {},
"outputs": [],
"source": [
"# Dropping the 'Tax' column\n",
"df.drop('Tax', axis=1)"
]
},
{
"cell_type": "markdown",
"id": "fc50f564",
"metadata": {},
"source": [
"### 9. Grouping Data\n",
"Use groupby() to group data and apply aggregation functions."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0c8c090",
"metadata": {},
"outputs": [],
"source": [
"# Grouping by 'City' and calculating the mean age\n",
"city_group = df.groupby('City')['Age'].mean()\n",
"city_group"
]
},
{
"cell_type": "markdown",
"id": "9389eede",
"metadata": {},
"source": [
"### 10. Merging DataFrames\n",
"You can combine DataFrames using merge()."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b0653041",
"metadata": {},
"outputs": [],
"source": [
"data_new = {'Name': ['Alice', 'Bob'], 'Bonus': [5000, 7000]}\n",
"df_bonus = pd.DataFrame(data_new)\n",
"\n",
"# Merging DataFrames\n",
"merged_df = pd.merge(df, df_bonus, on='Name', how='left')\n",
"merged_df"
]
},
{
"cell_type": "markdown",
"id": "e45b743d",
"metadata": {},
"source": [
"### 11. Sorting Data\n",
"You can sort a DataFrame using sort_values()."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09bc2365",
"metadata": {},
"outputs": [],
"source": [
"# Sorting by 'Age'\n",
"df.sort_values(by='Age')"
]
},
{
"cell_type": "markdown",
"id": "433ec31a",
"metadata": {},
"source": [
"### 12. Handling Missing Data\n",
"You can handle missing data using isnull(), fillna(), and dropna()."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8fd6f730",
"metadata": {},
"outputs": [],
"source": [
"# Checking for missing data\n",
"df.isnull()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d97983e",
"metadata": {},
"outputs": [],
"source": [
"# Filling missing values\n",
"df.fillna(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a5e6529",
"metadata": {},
"outputs": [],
"source": [
"# Dropping rows with missing values\n",
"df.dropna()"
]
},
{
"cell_type": "markdown",
"id": "3e8505cd",
"metadata": {},
"source": [
"### 13. Applying Functions to Columns\n",
"You can apply custom functions to DataFrame columns using apply()."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41c91fb4",
"metadata": {},
"outputs": [],
"source": [
"# Applying a lambda function to increase salary by 10%\n",
"df['Salary'] = df['Salary'].apply(lambda x: x * 1.1)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "e2aab900",
"metadata": {},
"source": [
"### 14. Renaming Columns\n",
"You can rename columns using rename()."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c5c5207",
"metadata": {},
"outputs": [],
"source": [
"# Renaming 'Salary' to 'Income'\n",
"df.rename(columns={'Salary': 'Income'}, inplace=True)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "09fdd950",
"metadata": {},
"source": [
"### 15. Pivot Tables\n",
"You can create pivot tables using pivot_table()."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c156016",
"metadata": {},
"outputs": [],
"source": [
"# Creating a pivot table showing average age by city\n",
"df.pivot_table(values='Age', index='City', aggfunc='mean')"
]
},
{
"cell_type": "markdown",
"id": "aad412dd",
"metadata": {},
"source": [
"### 16. Filtering with Multiple Conditions\n",
"You can filter data using multiple conditions combined with logical operators."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a441dde3",
"metadata": {},
"outputs": [],
"source": [
"# Filter rows where Age is greater than 25 and Salary is greater than 55,000\n",
"df[(df['Age'] > 25) & (df['Income'] > 55000)]"
]
},
{
"cell_type": "markdown",
"id": "aa87a5a0",
"metadata": {},
"source": [
"### 17. Interpolation\n",
"You can fill missing data points using interpolation with interpolate()."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "802cdf09",
"metadata": {},
"outputs": [],
"source": [
"# Filling missing values using interpolation\n",
"df['Age'].interpolate()"
]
},
{
"cell_type": "markdown",
"id": "6543418d",
"metadata": {},
"source": [
"### 18. Removing Duplicates\n",
"You can remove duplicate rows using drop_duplicates()."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72325c0c",
"metadata": {},
"outputs": [],
"source": [
"# Removing duplicate rows\n",
"df.drop_duplicates()"
]
},
{
"cell_type": "markdown",
"id": "35152cf4",
"metadata": {},
"source": [
"### 19. Replacing Values\n",
"You can replace specific values using replace()."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "045752aa",
"metadata": {},
"outputs": [],
"source": [
"# Replacing 'New York' with 'NYC'\n",
"df['City'].replace('New York', 'NYC', inplace=True)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "25807ba6",
"metadata": {},
"source": [
"### 20. Detecting and Removing Outliers\n",
"Outliers can be identified and removed based on certain statistical thresholds."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9ad63ee",
"metadata": {},
"outputs": [],
"source": [
"# Removing rows where Age is more than 3 standard deviations away from the mean\n",
"age_mean = df['Age'].mean()\n",
"age_std = df['Age'].std()\n",
"df_filtered = df[(df['Age'] > (age_mean - 3 * age_std)) & (df['Age'] < (age_mean + 3 * age_std))]\n",
"df_filtered"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
add more functions to it and improve the heading for each function
Sure! Let's enhance the notebook by adding some more functions and refining the headings for clarity and comprehension. Here is the expanded version of the notebook in JSON format:
{
"cells": [
{
"cell_type": "markdown",
"id": "c922f2ad",
"metadata": {},
"source": [
"# Comprehensive Guide to Basic Pandas Functions"
]
},
{
"cell_type": "markdown",
"id": "162663b9",
"metadata": {},
"source": [
"## 1. Import Pandas\n",
"The first step in using pandas is to import it into your Python script."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "37aec462",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"id": "e9ebc191",
"metadata": {},
"source": [
"## 2. Create a DataFrame\n",
"The DataFrame is the central data structure in pandas, allowing you to store and manipulate tabular data."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4231e02",
"metadata": {},
"outputs": [],
"source": [
"data = {\n",
" 'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],\n",
" 'Age': [24, 27, 22, 32, 29],\n",
" 'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],\n",
" 'Salary': [50000, 60000, 55000, 62000, 58000]\n",
"}\n",
"\n",
"df = pd.DataFrame(data)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "af5ff40d",
"metadata": {},
"source": [
"## 3. Load Data from Files\n",
"Pandas can read data from various file formats, such as CSV, Excel, and SQL databases."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4cef1cbc",
"metadata": {},
"outputs": [],
"source": [
"# df = pd.read_csv('sample_data.csv')\n",
"# df"
]
},
{
"cell_type": "markdown",
"id": "72082be0",
"metadata": {},
"source": [
"## 4. Basic Data Insight Functions\n",
"Quickly inspect your data with functions like `head()`, `info()`, and `describe()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f3b1f9f",
"metadata": {},
"outputs": [],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd75461e",
"metadata": {},
"outputs": [],
"source": [
"df.info()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c560de9b",
"metadata": {},
"outputs": [],
"source": [
"df.describe()"
]
},
{
"cell_type": "markdown",
"id": "bde1a93c",
"metadata": {},
"source": [
"## 5. Select Data\n",
"Select specific data using label-based (`loc`) or position-based (`iloc`) indexing."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e9ca66d",
"metadata": {},
"outputs": [],
"source": [
"# Select by label\n",
"df.loc[0, 'Name']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7f8be0f",
"metadata": {},
"outputs": [],
"source": [
"# Select by index position\n",
"df.iloc[0, 0]"
]
},
{
"cell_type": "markdown",
"id": "c142ba87",
"metadata": {},
"source": [
"## 6. Filter Data on Conditions\n",
"Filter your DataFrame based on conditional expressions."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91b8d523",
"metadata": {},
"outputs": [],
"source": [
"# Filter rows where age is greater than 25\n",
"df[df['Age'] > 25]"
]
},
{
"cell_type": "markdown",
"id": "65343816",
"metadata": {},
"source": [
"## 7. Add New Column\n",
"Adding new columns is straightforward in pandas."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65f6a0e8",
"metadata": {},
"outputs": [],
"source": [
"# Adding a new column 'Tax'\n",
"df['Tax'] = df['Salary'] * 0.1\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "d885c5cc",
"metadata": {},
"source": [
"## 8. Drop Columns or Rows\n",
"Remove unwanted data using the `drop()` function."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fd52007d",
"metadata": {},
"outputs": [],
"source": [
"# Dropping the 'Tax' column\n",
"df.drop('Tax', axis=1)"
]
},
{
"cell_type": "markdown",
"id": "fc50f564",
"metadata": {},
"source": [
"## 9. Group and Aggregate Data\n",
"Use `groupby()` for complex data manipulation by grouping."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0c8c090",
"metadata": {},
"outputs": [],
"source": [
"# Grouping by 'City' and calculating the mean age\n",
"city_group = df.groupby('City')['Age'].mean()\n",
"city_group"
]
},
{
"cell_type": "markdown",
"id": "9389eede",
"metadata": {},
"source": [
"## 10. Merge DataFrames\n",
"Combine data from different DataFrames with the `merge()` function."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b0653041",
"metadata": {},
"outputs": [],
"source": [
"data_new = {'Name': ['Alice', 'Bob'], 'Bonus': [5000, 7000]}\n",
"df_bonus = pd.DataFrame(data_new)\n",
"\n",
"# Merging DataFrames\n",
"merged_df = pd.merge(df, df_bonus, on='Name', how='left')\n",
"merged_df"
]
},
{
"cell_type": "markdown",
"id": "e45b743d",
"metadata": {},
"source": [
"## 11. Sort Data\n",
"Sort your DataFrame using `sort_values()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09bc2365",
"metadata": {},
"outputs": [],
"source": [
"# Sorting by 'Age'\n",
"df.sort_values(by='Age')"
]
},
{
"cell_type": "markdown",
"id": "433ec31a",
"metadata": {},
"source": [
"## 12. Handle Missing Data\n",
"Address missing data with functions like `isnull()`, `fillna()`, and `dropna()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8fd6f730",
"metadata": {},
"outputs": [],
"source": [
"# Checking for missing data\n",
"df.isnull()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7d97983e",
"metadata": {},
"outputs": [],
"source": [
"# Filling missing values\n",
"df.fillna(0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a5e6529",
"metadata": {},
"outputs": [],
"source": [
"# Dropping rows with missing values\n",
"df.dropna()"
]
},
{
"cell_type": "markdown",
"id": "3e8505cd",
"metadata": {},
"source": [
"## 13. Custom Function Application\n",
"Apply custom functions to DataFrame columns using the `apply()` method."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41c91fb4",
"metadata": {},
"outputs": [],
"source": [
"# Applying a lambda function to increase salary by 10%\n",
"df['Salary'] = df['Salary'].apply(lambda x: x * 1.1)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "e2aab900",
"metadata": {},
"source": [
"## 14. Rename Columns\n",
"Easily rename columns with the `rename()` method."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c5c5207",
"metadata": {},
"outputs": [],
"source": [
"# Renaming 'Salary' to 'Income'\n",
"df.rename(columns={'Salary': 'Income'}, inplace=True)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "09fdd950",
"metadata": {},
"source": [
"## 15. Construct Pivot Tables\n",
"Create pivot tables for multi-dimensional data analysis using `pivot_table()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c156016",
"metadata": {},
"outputs": [],
"source": [
"# Creating a pivot table showing average age by city\n",
"df.pivot_table(values='Age', index='City', aggfunc='mean')"
]
},
{
"cell_type": "markdown",
"id": "aad412dd",
"metadata": {},
"source": [
"## 16. Multi-condition Filtering\n",
"Filter your data based on multiple conditions using logical operators."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a441dde3",
"metadata": {},
"outputs": [],
"source": [
"# Filter rows where Age is greater than 25 and Salary is greater than 55,000\n",
"df[(df['Age'] > 25) & (df['Income'] > 55000)]"
]
},
{
"cell_type": "markdown",
"id": "aa87a5a0",
"metadata": {},
"source": [
"## 17. Interpolation for Missing Data\n",
"Fill missing data points through interpolation using `interpolate()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "802cdf09",
"metadata": {},
"outputs": [],
"source": [
"# Filling missing values using interpolation\n",
"df['Age'].interpolate()"
]
},
{
"cell_type": "markdown",
"id": "6543418d",
"metadata": {},
"source": [
"## 18. Eliminate Duplicates\n",
"Remove duplicate entries with the `drop_duplicates()` method."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72325c0c",
"metadata": {},
"outputs": [],
"source": [
"# Removing duplicate rows\n",
"df.drop_duplicates()"
]
},
{
"cell_type": "markdown",
"id": "35152cf4",
"metadata": {},
"source": [
"## 19. Replace Specific Values\n",
"Use `replace()` to change specific values in the DataFrame."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "045752aa",
"metadata": {},
"outputs": [],
"source": [
"# Replacing 'New York' with 'NYC'\n",
"df['City'].replace('New York', 'NYC', inplace=True)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "25807ba6",
"metadata": {},
"source": [
"## 20. Identify and Remove Outliers\n",
"Detect and handle outliers using statistical methods."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9ad63ee",
"metadata": {},
"outputs": [],
"source": [
"# Removing rows where Age is more than 3 standard deviations away from the mean\n",
"age_mean = df['Age'].mean()\n",
"age_std = df['Age'].std()\n",
"df_filtered = df[(df['Age'] > (age_mean - 3 * age_std)) & (df['Age'] < (age_mean + 3 * age_std))]\n",
"df_filtered"
]
},
{
"cell_type": "markdown",
"id": "new-function",
"metadata": {},
"source": [
"## 21. Reset Index\n",
"Reset the index of your DataFrame using `reset_index()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "reset_index",
"metadata": {},
"outputs": [],
"source": [
"# Resetting the index\n",
"df_reset = df.reset_index(drop=True)\n",
"df_reset"
]
},
{
"cell_type": "markdown",
"id": "new-function2",
"metadata": {},
"source": [
"## 22. Apply Transformations Using Map\n",
"Transform your DataFrame columns with `map()` for sophisticated mappings."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "map-function",
"metadata": {},
"outputs": [],
"source": [
"# Mapping a dictionary to transform 'City' values\n",
"city_map = {'NYC': 'New York', 'Los Angeles': 'LA', 'Chicago': 'CH', 'Houston': 'HOU', 'Phoenix': 'PHX'}\n",
"df['City'] = df['City'].map(city_map)\n",
"df"
]
},
{
"cell_type": "markdown",
"id": "new-function3",
"metadata": {},
"source": [
"## 23. Reshape Data with Melt\n",
"Use `melt()` to transform your DataFrame from wide to long format."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "melt-function",
"metadata": {},
"outputs": [],
"source": [
"# Melting the DataFrame to long format\n",
"df_melted = df.melt(id_vars=['Name'], value_vars=['Age', 'Income'], var_name='Variable', value_name='Value')\n",
"df_melted"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
reset_index, map, and melt.To use this notebook:
.ipynb, such as Enhanced_Pandas_Tutorial.ipynb.The execution_count field being set to null in a Jupyter Notebook cell is actually the default state indicating that the cell has not been executed yet. It's typical to have null values for new notebooks or before executing cells in your notebook for the first time. However, if this is unexpectedly causing an error, here are a few solutions to address this issue:
Execute the Notebook:
execution_count will be updated with the order in which the cell was run.Review JSON Syntax:
.ipynb file, the syntax remains correct and unchanged. Issues often arise from incorrectly formatted JSON during the copy-paste process.Manually Populate execution_count:
null with integers starting from 1, incrementally, might resolve issues if your environment is incorrectly parsing these values.Check Jupyter Environment:
Use Jupyter Notebook Interface:
Verify Environment and Version Compatibility:
Here is a snippet if you need an example notebook structure starting fresh with valid JSON. This initializes cells without manual execution_count specification, as they will be managed upon your first notebook execution:
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pandas DataFrame Example"
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}\n",
"df = pd.DataFrame(data)\n",
"df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
This small snippet can be expanded, ensuring each function or section you'd like to add is carefully structured in the same way, then adjusting upon loading the entire notebook into Jupyter.
Not yet included in a published release.