{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Pandas DataFrame.ipynb",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Hf7R3hwdq7Cf",
"colab_type": "text"
},
"source": [
"# Python Pandas DataFrame basics."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2nccJFNfr9CR",
"colab_type": "text"
},
"source": [
"## Let us understand the basics of Pandas DataFrame from scratch."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AYJqLgYsqtnX",
"colab_type": "text"
},
"source": [
"![alt text](https://miro.medium.com/max/1920/1*V0a5jxOmbBfQlKhtShKwfA.jpeg)\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gNnM4vBVrI6O",
"colab_type": "text"
},
"source": [
"Before getting started let me introduce you [Pandas](https://pandas.pydata.org/), Pandas is a python library which provided high-performance, easy-to-use data structures such as a series, Data Frame and Panel for data analysis tools for Python programming language. Moreover Pandas Data Frame consists of main components, the data, rows and columns. To use the pandas library and its data structures, all you have to do it to install it and import it. See the documentation of the Pandas library for a better understanding and installing guidance."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RLkI3b_3x1v6",
"colab_type": "text"
},
"source": [
"## Basic operations that can be appilied on a pandas Data Frame are as shown below:\n",
"\n",
"1. Creating a Data Frame.\n",
"2. Performing operations on Rows and Columns.\n",
"3. Data Selection, addition, deletion.\n",
"4. Working with missing data.\n",
"5. Renaming the Columns or Indices of a DataFrame."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5V_LbZXw6WBL",
"colab_type": "text"
},
"source": [
"## 1. Creating a pandas DataFrame"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c7BHKnbashFG",
"colab_type": "text"
},
"source": [
"The pandas data frame can be created by loading the data from the external, existing storage like a database, SQL or CSV files. But the pandas Data Frame can also be created from the lists, dictionary, etc. One of the ways to create a pandas data frame is shown below:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "5ke_cWSh6Ovg",
"colab_type": "code",
"outputId": "7b134526-01e6-4f0a-e4c6-b4fa6f4f7e5f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
}
},
"source": [
"# import the pandas library\n",
"import pandas as pd\n",
"# Dictionary of key pair values called data\n",
"data = {'Name':['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh'],\n",
" 'Age': [24, 23, 22, 19, 10]}\n",
"data"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Age': [24, 23, 22, 19, 10],\n",
" 'Name': ['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh']}"
]
},
"metadata": {
"tags": []
},
"execution_count": 46
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "KPm9UHy260Mk",
"colab_type": "code",
"outputId": "3701b345-ce5d-425a-fc1a-1f4d144d6d72",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
}
},
"source": [
"# Calling the pandas data frame method by passing the dictionary (data) as a parameter\n",
"df = pd.DataFrame(data)\n",
"df"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ashika | \n",
" 24 | \n",
"
\n",
" \n",
" 1 | \n",
" Tanu | \n",
" 23 | \n",
"
\n",
" \n",
" 2 | \n",
" Ashwin | \n",
" 22 | \n",
"
\n",
" \n",
" 3 | \n",
" Mohit | \n",
" 19 | \n",
"
\n",
" \n",
" 4 | \n",
" Sourabh | \n",
" 10 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"0 Ashika 24\n",
"1 Tanu 23\n",
"2 Ashwin 22\n",
"3 Mohit 19\n",
"4 Sourabh 10"
]
},
"metadata": {
"tags": []
},
"execution_count": 47
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "31PD5o5CuMg1",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oN_3wgjwuSCj",
"colab_type": "text"
},
"source": [
"## 2. Performing operations on Rows and Columns."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IYmC7xWFucju",
"colab_type": "text"
},
"source": [
"Data Frame is a two-dimensional data structure, data is stored in rows and columns. Below we can perform some operations on Rows and Columns."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9MgCQNfPu3pZ",
"colab_type": "text"
},
"source": [
"**Selecting a Column**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Dn4tXXWSvV4Y",
"colab_type": "text"
},
"source": [
"In order to select a particular column, all we can do is just call the name of the column inside the data frame."
]
},
{
"cell_type": "code",
"metadata": {
"id": "lCm4-F5lu-AJ",
"colab_type": "code",
"outputId": "55593c87-116b-4109-b190-680f8d06858e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
}
},
"source": [
"# import the pandas library\n",
"import pandas as pd\n",
"# Dictionary of key pair values called data\n",
"data = {'Name':['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh'],\n",
" 'Age': [24, 23, 22, 19, 10]}\n",
"data"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Age': [24, 23, 22, 19, 10],\n",
" 'Name': ['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh']}"
]
},
"metadata": {
"tags": []
},
"execution_count": 48
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "D8i1V1XqvD_3",
"colab_type": "code",
"outputId": "6bba316f-3628-46eb-8af5-ade5eacbfa83",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
}
},
"source": [
"# Calling the pandas data frame method by passing the dictionary (data) as a parameter\n",
"df = pd.DataFrame(data)\n",
"# Selecting column\n",
"df[['Name']]"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ashika | \n",
"
\n",
" \n",
" 1 | \n",
" Tanu | \n",
"
\n",
" \n",
" 2 | \n",
" Ashwin | \n",
"
\n",
" \n",
" 3 | \n",
" Mohit | \n",
"
\n",
" \n",
" 4 | \n",
" Sourabh | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name\n",
"0 Ashika\n",
"1 Tanu\n",
"2 Ashwin\n",
"3 Mohit\n",
"4 Sourabh"
]
},
"metadata": {
"tags": []
},
"execution_count": 49
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ROBV-5d9vkcZ",
"colab_type": "text"
},
"source": [
"**Selecting a Row**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3znYwir7vsmW",
"colab_type": "text"
},
"source": [
"Pandas Data Frame provides a method called \"loc\" which is used to retrive rows from the data frame. Also rows can also be selected by using the \"iloc\" as an function."
]
},
{
"cell_type": "code",
"metadata": {
"id": "BrWoRJMGwBaU",
"colab_type": "code",
"outputId": "86c0bf65-55b6-42ad-84c1-af6729ea88e3",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
}
},
"source": [
"# import the pandas library\n",
"import pandas as pd\n",
"# Dictionary of key pair values called data\n",
"data = {'Name':['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh'],\n",
" 'Age': [24, 23, 22, 19, 10]}\n",
"data"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Age': [24, 23, 22, 19, 10],\n",
" 'Name': ['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh']}"
]
},
"metadata": {
"tags": []
},
"execution_count": 50
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "jQmI0eTkwFfG",
"colab_type": "code",
"outputId": "0746f302-d51a-4426-c031-84f34f578287",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
}
},
"source": [
"# Calling the pandas data frame method by passing the dictionary (data) as a parameter\n",
"df = pd.DataFrame(data)\n",
"# Selecting a row\n",
"row = df.loc[1]\n",
"row\n"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Name Tanu\n",
"Age 23\n",
"Name: 1, dtype: object"
]
},
"metadata": {
"tags": []
},
"execution_count": 51
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0HTWcEydwVs_",
"colab_type": "text"
},
"source": [
"To select a particular column, all we can do is just call the name of the column inside the data frame. As seen above to work with the “loc” method you have to pass the index of the data frame as a parameter. The loc method accepts only integers as a parameter. \n",
"So in the above example, I wanted to access “Tanu” row, so I passed the index as 1 as a parameter. Now theres a quick assignment for you guys, use the \"iloc\" method and tell me the result."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "pRYy73kPw8CT",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tfnC875qxIwN",
"colab_type": "text"
},
"source": [
"## 3. Data Selection, addition, deletion."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_kHttYErxKlu",
"colab_type": "text"
},
"source": [
"You can treat a DataFrame semantically like a dict of like-indexed Series objects. Getting, setting, and deleting columns works with the same syntax as the analogous dict operations:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "A-YM5-OYysJR",
"colab_type": "code",
"outputId": "cc54d0da-3a19-41c2-decc-e4bdd2932979",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
}
},
"source": [
"# import the pandas library\n",
"import pandas as pd\n",
"# Dictionary of key pair values called data\n",
"data = {'Name':['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh'],\n",
" 'Age': [24, 23, 22, 19, 10]}\n",
"data"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Age': [24, 23, 22, 19, 10],\n",
" 'Name': ['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh']}"
]
},
"metadata": {
"tags": []
},
"execution_count": 52
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "H6-5ouINyt_y",
"colab_type": "code",
"outputId": "d473b0a4-896a-4c2e-cfd5-0414bd9baf46",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 121
}
},
"source": [
"# Calling the pandas data frame method by passing the dictionary (data) as a parameter\n",
"df = pd.DataFrame(data)\n",
"# Selecting the data from the column\n",
"df['Age']"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0 24\n",
"1 23\n",
"2 22\n",
"3 19\n",
"4 10\n",
"Name: Age, dtype: int64"
]
},
"metadata": {
"tags": []
},
"execution_count": 53
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wtv11VxZzA90",
"colab_type": "text"
},
"source": [
"Columns can be deleted like with a dict:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "cVH6-WCdzBmk",
"colab_type": "code",
"outputId": "1472e432-c1fe-4a80-dfb2-70a050aedd45",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
}
},
"source": [
"del df['Age']\n",
"df"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ashika | \n",
"
\n",
" \n",
" 1 | \n",
" Tanu | \n",
"
\n",
" \n",
" 2 | \n",
" Ashwin | \n",
"
\n",
" \n",
" 3 | \n",
" Mohit | \n",
"
\n",
" \n",
" 4 | \n",
" Sourabh | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name\n",
"0 Ashika\n",
"1 Tanu\n",
"2 Ashwin\n",
"3 Mohit\n",
"4 Sourabh"
]
},
"metadata": {
"tags": []
},
"execution_count": 54
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nz7_e1NGzRhY",
"colab_type": "text"
},
"source": [
"Data can be added by using the insert function. The insert function is available to insert at a particular location in the columns:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "oIehuXrPzga7",
"colab_type": "code",
"outputId": "9ad2ab7a-ff6f-48db-c712-8be74e748351",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
}
},
"source": [
"df.insert(1, 'name', df['Name'])\n",
"df"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" name | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ashika | \n",
" Ashika | \n",
"
\n",
" \n",
" 1 | \n",
" Tanu | \n",
" Tanu | \n",
"
\n",
" \n",
" 2 | \n",
" Ashwin | \n",
" Ashwin | \n",
"
\n",
" \n",
" 3 | \n",
" Mohit | \n",
" Mohit | \n",
"
\n",
" \n",
" 4 | \n",
" Sourabh | \n",
" Sourabh | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name name\n",
"0 Ashika Ashika\n",
"1 Tanu Tanu\n",
"2 Ashwin Ashwin\n",
"3 Mohit Mohit\n",
"4 Sourabh Sourabh"
]
},
"metadata": {
"tags": []
},
"execution_count": 55
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gZeouc020LYL",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DuXSNw0n0RSZ",
"colab_type": "text"
},
"source": [
"## 4. Working with missing data."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FS9roj5U0bof",
"colab_type": "text"
},
"source": [
"Missing data occur a lot of times when we are accessing big data sets. It occurs often like NaN (Not a number). In order to fill those values we can use \"isnull()\" method. This method checks whether a null value is present in a data frame or not."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0V1bFutn70pT",
"colab_type": "text"
},
"source": [
"**Checking for the missing values.**"
]
},
{
"cell_type": "code",
"metadata": {
"id": "9JiKwA8O7opN",
"colab_type": "code",
"outputId": "6ceb9a73-c9ce-4672-bfb3-0cce0af890a8",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"# importing both pandas and numpy libraries\n",
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"# Dictionary of key pair values called data\n",
"data ={'Name':['Tanu', np.nan],\n",
" 'Age': [23, np.nan]}\n",
"data"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'Age': [23, nan], 'Name': ['Tanu', nan]}"
]
},
"metadata": {
"tags": []
},
"execution_count": 56
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "YPEpn7an8Ihk",
"colab_type": "code",
"outputId": "375f49e9-216a-4b97-88b0-aed7e44bcfa7",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 110
}
},
"source": [
"df = pd.DataFrame(data)\n",
"df"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Tanu | \n",
" 23.0 | \n",
"
\n",
" \n",
" 1 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"0 Tanu 23.0\n",
"1 NaN NaN"
]
},
"metadata": {
"tags": []
},
"execution_count": 57
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Na-oPjl78MKU",
"colab_type": "code",
"outputId": "5ec7d8bc-a3f9-415e-8c1a-cfeaddf8ef07",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 110
}
},
"source": [
"# using the isnull() function\n",
"df.isnull()"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" False | \n",
" False | \n",
"
\n",
" \n",
" 1 | \n",
" True | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"0 False False\n",
"1 True True"
]
},
"metadata": {
"tags": []
},
"execution_count": 58
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8W-j0QLy8Z2h",
"colab_type": "text"
},
"source": [
"Now we have found the missing values, next task is to fill those values with 0 this can be done as shown below:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "qCC-YN5j8VZ2",
"colab_type": "code",
"outputId": "3c1a9e31-aebc-4310-8fa8-daf5b4ac05e0",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 110
}
},
"source": [
"df.fillna(0)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Tanu | \n",
" 23.0 | \n",
"
\n",
" \n",
" 1 | \n",
" 0 | \n",
" 0.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"0 Tanu 23.0\n",
"1 0 0.0"
]
},
"metadata": {
"tags": []
},
"execution_count": 59
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wVPkhcJh2riP",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T-gOxevd2sOt",
"colab_type": "text"
},
"source": [
"## 5. Renaming the Columns or Indices of a DataFrame.."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vxtuVjNc4qjt",
"colab_type": "text"
},
"source": [
"To give the columns or the index values of your data frame a different value, it’s best to use the .rename() method. Purposefully I have changed the column name to give a better insight"
]
},
{
"cell_type": "code",
"metadata": {
"id": "j5KyQ2ua3172",
"colab_type": "code",
"outputId": "d6488dda-015f-487a-ff47-09530d6de96e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
}
},
"source": [
"# import the pandas library\n",
"import pandas as pd\n",
"# Dictionary of key pair values called data\n",
"data = {'NAMe':['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh'],\n",
" 'AGe': [24, 23, 22, 19, 10]}\n",
"data"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{'AGe': [24, 23, 22, 19, 10],\n",
" 'NAMe': ['Ashika', 'Tanu', 'Ashwin', 'Mohit', 'Sourabh']}"
]
},
"metadata": {
"tags": []
},
"execution_count": 60
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "5zX7yrZS4zhg",
"colab_type": "code",
"outputId": "f76c6230-36f7-4065-a8bb-7950259ce5f8",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
}
},
"source": [
"# Calling the pandas data frame method by passing the dictionary (data) as a parameter\n",
"df = pd.DataFrame(data)\n",
"df"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" NAMe | \n",
" AGe | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ashika | \n",
" 24 | \n",
"
\n",
" \n",
" 1 | \n",
" Tanu | \n",
" 23 | \n",
"
\n",
" \n",
" 2 | \n",
" Ashwin | \n",
" 22 | \n",
"
\n",
" \n",
" 3 | \n",
" Mohit | \n",
" 19 | \n",
"
\n",
" \n",
" 4 | \n",
" Sourabh | \n",
" 10 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" NAMe AGe\n",
"0 Ashika 24\n",
"1 Tanu 23\n",
"2 Ashwin 22\n",
"3 Mohit 19\n",
"4 Sourabh 10"
]
},
"metadata": {
"tags": []
},
"execution_count": 61
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "csHahkx-5HgX",
"colab_type": "code",
"outputId": "39ee045a-4e25-4fe9-acf9-5cf1b5863655",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
}
},
"source": [
"newcols = {\n",
" 'NAMe': 'Name',\n",
" 'AGe': 'Age'\n",
" }\n",
"# Use `rename()` to rename your columns\n",
"df.rename(columns=newcols, inplace=True)\n",
"df"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ashika | \n",
" 24 | \n",
"
\n",
" \n",
" 1 | \n",
" Tanu | \n",
" 23 | \n",
"
\n",
" \n",
" 2 | \n",
" Ashwin | \n",
" 22 | \n",
"
\n",
" \n",
" 3 | \n",
" Mohit | \n",
" 19 | \n",
"
\n",
" \n",
" 4 | \n",
" Sourabh | \n",
" 10 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"0 Ashika 24\n",
"1 Tanu 23\n",
"2 Ashwin 22\n",
"3 Mohit 19\n",
"4 Sourabh 10"
]
},
"metadata": {
"tags": []
},
"execution_count": 62
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "aOQ-3zM75PjG",
"colab_type": "code",
"outputId": "cd84ca01-038b-452c-dfcc-da9e3462103b",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
}
},
"source": [
"# The values of new index\n",
"newindex = {\n",
" 0: 'a',\n",
" 1: 'b',\n",
" 2: 'c',\n",
" 3: 'd',\n",
" 4: 'e'\n",
"}\n",
"# Rename your index\n",
"df.rename(index=newindex)"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
"
\n",
" \n",
" \n",
" \n",
" a | \n",
" Ashika | \n",
" 24 | \n",
"
\n",
" \n",
" b | \n",
" Tanu | \n",
" 23 | \n",
"
\n",
" \n",
" c | \n",
" Ashwin | \n",
" 22 | \n",
"
\n",
" \n",
" d | \n",
" Mohit | \n",
" 19 | \n",
"
\n",
" \n",
" e | \n",
" Sourabh | \n",
" 10 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age\n",
"a Ashika 24\n",
"b Tanu 23\n",
"c Ashwin 22\n",
"d Mohit 19\n",
"e Sourabh 10"
]
},
"metadata": {
"tags": []
},
"execution_count": 63
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2spAtPD66CIE",
"colab_type": "text"
},
"source": [
"\n",
"\n",
"---\n",
"\n"
]
}
]
}