Skip to content

Instantly share code, notes, and snippets.

@MohdAzamSayeed
Created November 12, 2020 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohdAzamSayeed/1af580b296286779ae31ac6f5a1cc2ea to your computer and use it in GitHub Desktop.
Save MohdAzamSayeed/1af580b296286779ae31ac6f5a1cc2ea to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Name Age Address Qualification\n",
"0 Jai 27 Delhi Msc\n",
"1 Princi 24 Kanpur MA\n",
"2 Gaurav 22 Allahabad MCA\n",
"3 Anuj 32 Kannauj Phd\n",
" Name Qualification\n",
"0 Jai Msc\n",
"1 Princi MA\n",
"2 Gaurav MCA\n",
"3 Anuj Phd\n"
]
}
],
"source": [
"# Import pandas package\n",
"import pandas as pd\n",
" \n",
"# Define a dictionary containing employee data\n",
"data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],\n",
" 'Age':[27, 24, 22, 32],\n",
" 'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],\n",
" 'Qualification':['Msc', 'MA', 'MCA', 'Phd']}\n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df = pd.DataFrame(data)\n",
"print(df)\n",
" \n",
"# select two columns\n",
"print(df[['Name', 'Qualification']])"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>cobra</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>viper</th>\n",
" <td>4</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>sidewinder</th>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"cobra 1 2\n",
"viper 4 5\n",
"sidewinder 7 8"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],\n",
" index=['cobra', 'viper', 'sidewinder'],\n",
" columns=['max_speed', 'shield'])\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>cobra</th>\n",
" <td>2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" shield\n",
"cobra 2"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[['cobra'],['shield']]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"max_speed 1\n",
"shield 2\n",
"Name: cobra, dtype: int64"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc['cobra',['max_speed','shield']]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>viper</th>\n",
" <td>4</td>\n",
" <td>5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"viper 4 5"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[['viper']]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"viper 5\n",
"sidewinder 8\n",
"Name: shield, dtype: int64"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[['viper', 'sidewinder'],'shield']"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc['cobra', 'shield']"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"cobra 1\n",
"viper 4\n",
"Name: max_speed, dtype: int64"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc['cobra':'viper', 'max_speed']\n"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>sidewinder</th>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"sidewinder 7 8"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[[False, False, True]]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>sidewinder</th>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"sidewinder 7 8"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[df['shield'] > 6]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>sidewinder</th>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"sidewinder 7 8"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[df['shield'] > 6]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>sidewinder</th>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"sidewinder 7 8"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.query('shield > 6')"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>sidewinder</th>\n",
" <td>7</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed\n",
"sidewinder 7"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[df['shield'] > 6, ['max_speed']]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>cobra</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>viper</th>\n",
" <td>4</td>\n",
" <td>50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>sidewinder</th>\n",
" <td>7</td>\n",
" <td>50</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"cobra 1 2\n",
"viper 4 50\n",
"sidewinder 7 50"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[['viper', 'sidewinder'], ['shield']] = 50\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>cobra</th>\n",
" <td>10</td>\n",
" <td>10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>viper</th>\n",
" <td>4</td>\n",
" <td>50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>sidewinder</th>\n",
" <td>7</td>\n",
" <td>50</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"cobra 10 10\n",
"viper 4 50\n",
"sidewinder 7 50"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc['cobra'] = 10\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>cobra</th>\n",
" <td>30</td>\n",
" <td>10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>viper</th>\n",
" <td>30</td>\n",
" <td>50</td>\n",
" </tr>\n",
" <tr>\n",
" <th>sidewinder</th>\n",
" <td>30</td>\n",
" <td>50</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"cobra 30 10\n",
"viper 30 50\n",
"sidewinder 30 50"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[:, 'max_speed'] = 30\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>cobra</th>\n",
" <td>30</td>\n",
" <td>10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>viper</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>sidewinder</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"cobra 30 10\n",
"viper 0 0\n",
"sidewinder 0 0"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[df['shield'] > 35] = 0\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>max_speed</th>\n",
" <th>shield</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>4</td>\n",
" <td>5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" max_speed shield\n",
"7 1 2\n",
"8 4 5\n",
"9 7 8"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame([[1, 2], [4, 5], [7, 8]],\n",
" index=[7, 8, 9], columns=['max_speed', 'shield'])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 name aparna\n",
"degree MBA\n",
"score 90\n",
"Name: 0, dtype: object\n",
"\n",
"1 name pankaj\n",
"degree BCA\n",
"score 40\n",
"Name: 1, dtype: object\n",
"\n",
"2 name sudhir\n",
"degree M.Tech\n",
"score 80\n",
"Name: 2, dtype: object\n",
"\n",
"3 name Geeku\n",
"degree MBA\n",
"score 98\n",
"Name: 3, dtype: object\n",
"\n"
]
}
],
"source": [
"dict = {'name':[\"aparna\", \"pankaj\", \"sudhir\", \"Geeku\"],\n",
" 'degree': [\"MBA\", \"BCA\", \"M.Tech\", \"MBA\"],\n",
" 'score':[90, 40, 80, 98]}\n",
" \n",
"# creating a dataframe from a dictionary \n",
"df = pd.DataFrame(dict)\n",
" \n",
"# iterating over rows using iterrows() function \n",
"for i, j in df.iterrows():\n",
" print(i, j)\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['name', 'degree', 'score']\n",
"sudhir\n",
"M.Tech\n",
"80\n"
]
}
],
"source": [
"# creating a list of dataframe columns\n",
"columns = list(df)\n",
"print(columns)\n",
"for i in columns:\n",
" \n",
" # printing the third element of the column\n",
" print (df[i][2])"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Name Age Address Qualification\n",
"0 Jai 27 Nagpur Msc\n",
"1 Princi 24 Kanpur MA\n",
"2 Gaurav 22 Allahabad MCA\n",
"3 Anuj 32 Kannuaj Phd \n",
"\n",
" Name Age Address Qualification\n",
"4 Abhi 17 Nagpur Btech\n",
"5 Ayushi 14 Kanpur B.A\n",
"6 Dhiraj 12 Allahabad Bcom\n",
"7 Hitesh 52 Kannuaj B.hons\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Age</th>\n",
" <th>Address</th>\n",
" <th>Qualification</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Jai</td>\n",
" <td>27</td>\n",
" <td>Nagpur</td>\n",
" <td>Msc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Princi</td>\n",
" <td>24</td>\n",
" <td>Kanpur</td>\n",
" <td>MA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Gaurav</td>\n",
" <td>22</td>\n",
" <td>Allahabad</td>\n",
" <td>MCA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Anuj</td>\n",
" <td>32</td>\n",
" <td>Kannuaj</td>\n",
" <td>Phd</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Abhi</td>\n",
" <td>17</td>\n",
" <td>Nagpur</td>\n",
" <td>Btech</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Ayushi</td>\n",
" <td>14</td>\n",
" <td>Kanpur</td>\n",
" <td>B.A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Dhiraj</td>\n",
" <td>12</td>\n",
" <td>Allahabad</td>\n",
" <td>Bcom</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Hitesh</td>\n",
" <td>52</td>\n",
" <td>Kannuaj</td>\n",
" <td>B.hons</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name Age Address Qualification\n",
"0 Jai 27 Nagpur Msc\n",
"1 Princi 24 Kanpur MA\n",
"2 Gaurav 22 Allahabad MCA\n",
"3 Anuj 32 Kannuaj Phd\n",
"4 Abhi 17 Nagpur Btech\n",
"5 Ayushi 14 Kanpur B.A\n",
"6 Dhiraj 12 Allahabad Bcom\n",
"7 Hitesh 52 Kannuaj B.hons"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data1 = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'], \n",
" 'Age':[27, 24, 22, 32], \n",
" 'Address':['Nagpur', 'Kanpur', 'Allahabad', 'Kannuaj'], \n",
" 'Qualification':['Msc', 'MA', 'MCA', 'Phd']} \n",
" \n",
"# Define a dictionary containing employee data \n",
"data2 = {'Name':['Abhi', 'Ayushi', 'Dhiraj', 'Hitesh'], \n",
" 'Age':[17, 14, 12, 52], \n",
" 'Address':['Nagpur', 'Kanpur', 'Allahabad', 'Kannuaj'], \n",
" 'Qualification':['Btech', 'B.A', 'Bcom', 'B.hons']} \n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df = pd.DataFrame(data1,index=[0, 1, 2, 3])\n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df1 = pd.DataFrame(data2, index=[4, 5, 6, 7])\n",
" \n",
"print(df, \"\\n\\n\", df1) \n",
"frames = [df, df1]\n",
" \n",
"res1 = pd.concat(frames)\n",
"res1"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Name Age Address Qualification Mobile No\n",
"0 Jai 27 Nagpur Msc 97\n",
"1 Princi 24 Kanpur MA 91\n",
"2 Gaurav 22 Allahabad MCA 58\n",
"3 Anuj 32 Kannuaj Phd 76 \n",
"\n",
" Name Age Address Qualification Salary\n",
"2 Gaurav 22 Allahabad MCA 1000\n",
"3 Anuj 32 Kannuaj Phd 2000\n",
"6 Dhiraj 12 Allahabad Bcom 3000\n",
"7 Hitesh 52 Kannuaj B.hons 4000\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Age</th>\n",
" <th>Address</th>\n",
" <th>Qualification</th>\n",
" <th>Mobile No</th>\n",
" <th>Name</th>\n",
" <th>Age</th>\n",
" <th>Address</th>\n",
" <th>Qualification</th>\n",
" <th>Salary</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Gaurav</td>\n",
" <td>22</td>\n",
" <td>Allahabad</td>\n",
" <td>MCA</td>\n",
" <td>58</td>\n",
" <td>Gaurav</td>\n",
" <td>22</td>\n",
" <td>Allahabad</td>\n",
" <td>MCA</td>\n",
" <td>1000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Anuj</td>\n",
" <td>32</td>\n",
" <td>Kannuaj</td>\n",
" <td>Phd</td>\n",
" <td>76</td>\n",
" <td>Anuj</td>\n",
" <td>32</td>\n",
" <td>Kannuaj</td>\n",
" <td>Phd</td>\n",
" <td>2000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name Age Address Qualification Mobile No Name Age Address \\\n",
"2 Gaurav 22 Allahabad MCA 58 Gaurav 22 Allahabad \n",
"3 Anuj 32 Kannuaj Phd 76 Anuj 32 Kannuaj \n",
"\n",
" Qualification Salary \n",
"2 MCA 1000 \n",
"3 Phd 2000 "
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd \n",
" \n",
"# Define a dictionary containing employee data \n",
"data1 = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'], \n",
" 'Age':[27, 24, 22, 32], \n",
" 'Address':['Nagpur', 'Kanpur', 'Allahabad', 'Kannuaj'], \n",
" 'Qualification':['Msc', 'MA', 'MCA', 'Phd'],\n",
" 'Mobile No': [97, 91, 58, 76]} \n",
" \n",
"# Define a dictionary containing employee data \n",
"data2 = {'Name':['Gaurav', 'Anuj', 'Dhiraj', 'Hitesh'], \n",
" 'Age':[22, 32, 12, 52], \n",
" 'Address':['Allahabad', 'Kannuaj', 'Allahabad', 'Kannuaj'], \n",
" 'Qualification':['MCA', 'Phd', 'Bcom', 'B.hons'],\n",
" 'Salary':[1000, 2000, 3000, 4000]} \n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df = pd.DataFrame(data1,index=[0, 1, 2, 3])\n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df1 = pd.DataFrame(data2, index=[2, 3, 6, 7]) \n",
" \n",
"print(df, \"\\n\\n\", df1) \n",
"res2 = pd.concat([df, df1], axis=1, join='inner')\n",
" \n",
"res2"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Address</th>\n",
" <th>Age</th>\n",
" <th>Name</th>\n",
" <th>Qualification</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Nagpur</td>\n",
" <td>27</td>\n",
" <td>Jai</td>\n",
" <td>Msc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Kanpur</td>\n",
" <td>24</td>\n",
" <td>Princi</td>\n",
" <td>MA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Allahabad</td>\n",
" <td>22</td>\n",
" <td>Gaurav</td>\n",
" <td>MCA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Kannuaj</td>\n",
" <td>32</td>\n",
" <td>Anuj</td>\n",
" <td>Phd</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Allahabad</td>\n",
" <td>22</td>\n",
" <td>Gaurav</td>\n",
" <td>MCA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Kannuaj</td>\n",
" <td>32</td>\n",
" <td>Anuj</td>\n",
" <td>Phd</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Allahabad</td>\n",
" <td>12</td>\n",
" <td>Dhiraj</td>\n",
" <td>Bcom</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Kannuaj</td>\n",
" <td>52</td>\n",
" <td>Hitesh</td>\n",
" <td>B.hons</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Address Age Name Qualification\n",
"0 Nagpur 27 Jai Msc\n",
"1 Kanpur 24 Princi MA\n",
"2 Allahabad 22 Gaurav MCA\n",
"3 Kannuaj 32 Anuj Phd\n",
"2 Allahabad 22 Gaurav MCA\n",
"3 Kannuaj 32 Anuj Phd\n",
"6 Allahabad 12 Dhiraj Bcom\n",
"7 Kannuaj 52 Hitesh B.hons"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.concat([df, df1], join='inner',sort=True)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Age</th>\n",
" <th>Address</th>\n",
" <th>Qualification</th>\n",
" <th>Mobile No</th>\n",
" <th>Name</th>\n",
" <th>Age</th>\n",
" <th>Address</th>\n",
" <th>Qualification</th>\n",
" <th>Salary</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Jai</td>\n",
" <td>27.0</td>\n",
" <td>Nagpur</td>\n",
" <td>Msc</td>\n",
" <td>97.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Princi</td>\n",
" <td>24.0</td>\n",
" <td>Kanpur</td>\n",
" <td>MA</td>\n",
" <td>91.0</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Gaurav</td>\n",
" <td>22.0</td>\n",
" <td>Allahabad</td>\n",
" <td>MCA</td>\n",
" <td>58.0</td>\n",
" <td>Gaurav</td>\n",
" <td>22.0</td>\n",
" <td>Allahabad</td>\n",
" <td>MCA</td>\n",
" <td>1000.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Anuj</td>\n",
" <td>32.0</td>\n",
" <td>Kannuaj</td>\n",
" <td>Phd</td>\n",
" <td>76.0</td>\n",
" <td>Anuj</td>\n",
" <td>32.0</td>\n",
" <td>Kannuaj</td>\n",
" <td>Phd</td>\n",
" <td>2000.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>Dhiraj</td>\n",
" <td>12.0</td>\n",
" <td>Allahabad</td>\n",
" <td>Bcom</td>\n",
" <td>3000.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>Hitesh</td>\n",
" <td>52.0</td>\n",
" <td>Kannuaj</td>\n",
" <td>B.hons</td>\n",
" <td>4000.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name Age Address Qualification Mobile No Name Age Address \\\n",
"0 Jai 27.0 Nagpur Msc 97.0 NaN NaN NaN \n",
"1 Princi 24.0 Kanpur MA 91.0 NaN NaN NaN \n",
"2 Gaurav 22.0 Allahabad MCA 58.0 Gaurav 22.0 Allahabad \n",
"3 Anuj 32.0 Kannuaj Phd 76.0 Anuj 32.0 Kannuaj \n",
"6 NaN NaN NaN NaN NaN Dhiraj 12.0 Allahabad \n",
"7 NaN NaN NaN NaN NaN Hitesh 52.0 Kannuaj \n",
"\n",
" Qualification Salary \n",
"0 NaN NaN \n",
"1 NaN NaN \n",
"2 MCA 1000.0 \n",
"3 Phd 2000.0 \n",
"6 Bcom 3000.0 \n",
"7 B.hons 4000.0 "
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res2 = pd.concat([df, df1], axis=1, sort=False)\n",
" \n",
"res2"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Name Age Address Qualification\n",
"0 Jai 27 Nagpur Msc\n",
"1 Princi 24 Kanpur MA\n",
"2 Gaurav 22 Allahabad MCA\n",
"3 Anuj 32 Kannuaj Phd \n",
"\n",
" Name Age Address Qualification\n",
"4 Abhi 17 Nagpur Btech\n",
"5 Ayushi 14 Kanpur B.A\n",
"6 Dhiraj 12 Allahabad Bcom\n",
"7 Hitesh 52 Kannuaj B.hons\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Age</th>\n",
" <th>Address</th>\n",
" <th>Qualification</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Jai</td>\n",
" <td>27</td>\n",
" <td>Nagpur</td>\n",
" <td>Msc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Princi</td>\n",
" <td>24</td>\n",
" <td>Kanpur</td>\n",
" <td>MA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Gaurav</td>\n",
" <td>22</td>\n",
" <td>Allahabad</td>\n",
" <td>MCA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Anuj</td>\n",
" <td>32</td>\n",
" <td>Kannuaj</td>\n",
" <td>Phd</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>Abhi</td>\n",
" <td>17</td>\n",
" <td>Nagpur</td>\n",
" <td>Btech</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>Ayushi</td>\n",
" <td>14</td>\n",
" <td>Kanpur</td>\n",
" <td>B.A</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>Dhiraj</td>\n",
" <td>12</td>\n",
" <td>Allahabad</td>\n",
" <td>Bcom</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>Hitesh</td>\n",
" <td>52</td>\n",
" <td>Kannuaj</td>\n",
" <td>B.hons</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name Age Address Qualification\n",
"0 Jai 27 Nagpur Msc\n",
"1 Princi 24 Kanpur MA\n",
"2 Gaurav 22 Allahabad MCA\n",
"3 Anuj 32 Kannuaj Phd\n",
"4 Abhi 17 Nagpur Btech\n",
"5 Ayushi 14 Kanpur B.A\n",
"6 Dhiraj 12 Allahabad Bcom\n",
"7 Hitesh 52 Kannuaj B.hons"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data1 = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'], \n",
" 'Age':[27, 24, 22, 32], \n",
" 'Address':['Nagpur', 'Kanpur', 'Allahabad', 'Kannuaj'], \n",
" 'Qualification':['Msc', 'MA', 'MCA', 'Phd']} \n",
" \n",
"# Define a dictionary containing employee data \n",
"data2 = {'Name':['Abhi', 'Ayushi', 'Dhiraj', 'Hitesh'], \n",
" 'Age':[17, 14, 12, 52], \n",
" 'Address':['Nagpur', 'Kanpur', 'Allahabad', 'Kannuaj'], \n",
" 'Qualification':['Btech', 'B.A', 'Bcom', 'B.hons']} \n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df = pd.DataFrame(data1,index=[0, 1, 2, 3])\n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df1 = pd.DataFrame(data2, index=[4, 5, 6, 7])\n",
" \n",
"print(df, \"\\n\\n\", df1) \n",
"res = df.append(df1)\n",
"res"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Height</th>\n",
" <th>Qualification</th>\n",
" <th>Address</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Jai</td>\n",
" <td>5.1</td>\n",
" <td>Msc</td>\n",
" <td>Delhi</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Princi</td>\n",
" <td>6.2</td>\n",
" <td>MA</td>\n",
" <td>Bangalore</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Gaurav</td>\n",
" <td>5.1</td>\n",
" <td>Msc</td>\n",
" <td>Chennai</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Anuj</td>\n",
" <td>5.2</td>\n",
" <td>Msc</td>\n",
" <td>Patna</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name Height Qualification Address\n",
"0 Jai 5.1 Msc Delhi\n",
"1 Princi 6.2 MA Bangalore\n",
"2 Gaurav 5.1 Msc Chennai\n",
"3 Anuj 5.2 Msc Patna"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'], \n",
" 'Height': [5.1, 6.2, 5.1, 5.2], \n",
" 'Qualification': ['Msc', 'MA', 'Msc', 'Msc']} \n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df = pd.DataFrame(data) \n",
" \n",
"# Declare a list that is to be converted into a column \n",
"address = ['Delhi', 'Bangalore', 'Chennai', 'Patna'] \n",
" \n",
"# Using 'Address' as the column name \n",
"# and equating it to the list \n",
"df['Address'] = address \n",
" \n",
"# Observe the result \n",
"df "
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Height</th>\n",
" <th>Age</th>\n",
" <th>Qualification</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Jai</td>\n",
" <td>5.1</td>\n",
" <td>21</td>\n",
" <td>Msc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Princi</td>\n",
" <td>6.2</td>\n",
" <td>23</td>\n",
" <td>MA</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Gaurav</td>\n",
" <td>5.1</td>\n",
" <td>24</td>\n",
" <td>Msc</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Anuj</td>\n",
" <td>5.2</td>\n",
" <td>21</td>\n",
" <td>Msc</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name Height Age Qualification\n",
"0 Jai 5.1 21 Msc\n",
"1 Princi 6.2 23 MA\n",
"2 Gaurav 5.1 24 Msc\n",
"3 Anuj 5.2 21 Msc"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'], \n",
" 'Height': [5.1, 6.2, 5.1, 5.2], \n",
" 'Qualification': ['Msc', 'MA', 'Msc', 'Msc']} \n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df = pd.DataFrame(data) \n",
" \n",
"# Using DataFrame.insert() to add a column \n",
"df.insert(2, \"Age\", [21, 23, 24, 21], True) \n",
" \n",
"# Observe the result \n",
"df "
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Name</th>\n",
" <th>Height</th>\n",
" <th>Qualification</th>\n",
" <th>address</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Jai</td>\n",
" <td>5.1</td>\n",
" <td>Msc</td>\n",
" <td>Delhi</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Princi</td>\n",
" <td>6.2</td>\n",
" <td>MA</td>\n",
" <td>Bangalore</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Gaurav</td>\n",
" <td>5.1</td>\n",
" <td>Msc</td>\n",
" <td>Chennai</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Anuj</td>\n",
" <td>5.2</td>\n",
" <td>Msc</td>\n",
" <td>Patna</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Name Height Qualification address\n",
"0 Jai 5.1 Msc Delhi\n",
"1 Princi 6.2 MA Bangalore\n",
"2 Gaurav 5.1 Msc Chennai\n",
"3 Anuj 5.2 Msc Patna"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'], \n",
" 'Height': [5.1, 6.2, 5.1, 5.2], \n",
" 'Qualification': ['Msc', 'MA', 'Msc', 'Msc']} \n",
" \n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df = pd.DataFrame(data) \n",
" \n",
"# Using 'Address' as the column name and equating it to the list \n",
"df2 = df.assign(address = ['Delhi', 'Bangalore', 'Chennai', 'Patna']) \n",
" \n",
"# Observe the result \n",
"df2 "
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>course_name</th>\n",
" <th>student_name</th>\n",
" <th>student_city</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Data Structures</td>\n",
" <td>A</td>\n",
" <td>Chennai</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Python</td>\n",
" <td>B</td>\n",
" <td>Pune</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Machine Learning</td>\n",
" <td>C</td>\n",
" <td>Delhi</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" course_name student_name student_city\n",
"0 Data Structures A Chennai\n",
"1 Python B Pune\n",
"2 Machine Learning C Delhi"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.DataFrame({ \n",
" 'course_name': ['Data Structures', 'Python', \n",
" 'Machine Learning'], \n",
" 'student_name': ['A', 'B', \n",
" 'C'], \n",
" 'student_city': ['Chennai', 'Pune', \n",
" 'Delhi'], \n",
" 'student_gender': ['M', 'F', \n",
" 'M'] }) \n",
"# show the Dataframe \n",
"data\n",
"data = pd.DataFrame({ \n",
" 'course_name': ['Data Structures', 'Python', \n",
" 'Machine Learning'], \n",
" 'student_name': ['A', 'B', \n",
" 'C'], \n",
" 'student_city': ['Chennai', 'Pune', \n",
" 'Delhi'], \n",
" 'student_gender': ['M', 'F', \n",
" 'M'] }) \n",
" \n",
"df = data.loc[ : , data.columns != 'student_gender'] \n",
" \n",
"# show the dataframe \n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>course_name</th>\n",
" <th>student_name</th>\n",
" <th>student_gender</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Data Structures</td>\n",
" <td>A</td>\n",
" <td>M</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Python</td>\n",
" <td>B</td>\n",
" <td>F</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Machine Learning</td>\n",
" <td>C</td>\n",
" <td>M</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" course_name student_name student_gender\n",
"0 Data Structures A M\n",
"1 Python B F\n",
"2 Machine Learning C M"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.DataFrame({ \n",
" 'course_name': ['Data Structures', 'Python', \n",
" 'Machine Learning'], \n",
" \n",
" 'student_name': ['A', 'B', \n",
" 'C'], \n",
" \n",
" 'student_city': ['Chennai', 'Pune', \n",
" 'Delhi'], \n",
" \n",
" 'student_gender': ['M', 'F', \n",
" 'M'] }) \n",
" \n",
"# drop method \n",
"df = data.drop('student_city', \n",
" axis = 1) \n",
" \n",
"# show the dataframe \n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>course_name</th>\n",
" <th>student_city</th>\n",
" <th>student_gender</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Data Structures</td>\n",
" <td>Chennai</td>\n",
" <td>M</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Python</td>\n",
" <td>Pune</td>\n",
" <td>F</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Machine Learning</td>\n",
" <td>Delhi</td>\n",
" <td>M</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" course_name student_city student_gender\n",
"0 Data Structures Chennai M\n",
"1 Python Pune F\n",
"2 Machine Learning Delhi M"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = pd.DataFrame({ \n",
" 'course_name': ['Data Structures', 'Python', \n",
" 'Machine Learning'], \n",
" 'student_name': ['A', 'B', \n",
" 'C'], \n",
" 'student_city': ['Chennai', 'Pune', \n",
" 'Delhi'], \n",
" 'student_gender': ['M', 'F', \n",
" 'M'] }) \n",
" \n",
"df = data[data.columns.difference(['student_name'])] \n",
" \n",
"# show the dataframe \n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['course_name', 'student_city', 'student_gender'], dtype='object')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.columns.difference(['student_name'])"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>A</th>\n",
" <th>B</th>\n",
" <th>E</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>A1</td>\n",
" <td>B1</td>\n",
" <td>E1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>A2</td>\n",
" <td>B2</td>\n",
" <td>E2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>A3</td>\n",
" <td>B3</td>\n",
" <td>E3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>A4</td>\n",
" <td>B4</td>\n",
" <td>E4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>A5</td>\n",
" <td>B5</td>\n",
" <td>E5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" A B E\n",
"0 A1 B1 E1\n",
"1 A2 B2 E2\n",
"2 A3 B3 E3\n",
"3 A4 B4 E4\n",
"4 A5 B5 E5"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = { \n",
" 'A':['A1', 'A2', 'A3', 'A4', 'A5'], \n",
" 'B':['B1', 'B2', 'B3', 'B4', 'B5'], \n",
" 'C':['C1', 'C2', 'C3', 'C4', 'C5'], \n",
" 'D':['D1', 'D2', 'D3', 'D4', 'D5'], \n",
" 'E':['E1', 'E2', 'E3', 'E4', 'E5'] } \n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df = pd.DataFrame(data) \n",
" \n",
"# Remove two columns name is 'C' and 'D' \n",
"df.drop(['C', 'D'], axis=1) \n",
" \n",
"# df.drop(columns =['C', 'D']) \n"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>B</th>\n",
" <th>C</th>\n",
" <th>D</th>\n",
" <th>E</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>B1</td>\n",
" <td>C1</td>\n",
" <td>D1</td>\n",
" <td>E1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>B2</td>\n",
" <td>C2</td>\n",
" <td>D2</td>\n",
" <td>E2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>B3</td>\n",
" <td>C3</td>\n",
" <td>D3</td>\n",
" <td>E3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>B4</td>\n",
" <td>C4</td>\n",
" <td>D4</td>\n",
" <td>E4</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>B5</td>\n",
" <td>C5</td>\n",
" <td>D5</td>\n",
" <td>E5</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" B C D E\n",
"0 B1 C1 D1 E1\n",
"1 B2 C2 D2 E2\n",
"2 B3 C3 D3 E3\n",
"3 B4 C4 D4 E4\n",
"4 B5 C5 D5 E5"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = { \n",
" 'A':['A1', 'A2', 'A3', 'A4', 'A5'], \n",
" 'B':['B1', 'B2', 'B3', 'B4', 'B5'], \n",
" 'C':['C1', 'C2', 'C3', 'C4', 'C5'], \n",
" 'D':['D1', 'D2', 'D3', 'D4', 'D5'], \n",
" 'E':['E1', 'E2', 'E3', 'E4', 'E5'] } \n",
" \n",
"# Convert the dictionary into DataFrame \n",
"df = pd.DataFrame(data) \n",
"for col in df.columns: \n",
" if 'A' in col: \n",
" del df[col] \n",
" \n",
"df "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
" \n",
"# making data frame from csv file\n",
"data = pd.read_csv(\"D:\\\\data\\\\nba.csv\", index_col =\"Name\")\n",
" \n",
"# retrieving row by loc method\n",
"first = data.loc[\"Avery Bradley\"]\n",
"second = data.loc[\"R.J. Hunter\"]\n",
" \n",
" \n",
"print(first, \"\\n\\n\\n\", second)"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment