0% found this document useful (0 votes)
6 views3 pages

Untitledd

Uploaded by

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

Untitledd

Uploaded by

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

{

"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "f8360559",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Grade Points\n",
"0 A 4\n",
"1 B 3\n",
"2 C 2\n",
"3 A 4\n",
"4 B 3\n"
]
}
],
"source": [
"import pandas as pd\n",
"\n",
"# Example DataFrame\n",
"data = {'Grade': ['A', 'B', 'C', 'A', 'B']}\n",
"df = pd.DataFrame(data)\n",
"\n",
"# Dictionary to map grades to points\n",
"grade_mapping = {'A': 4, 'B': 3, 'C': 2}\n",
"\n",
"# Mapping grades to points using map\n",
"df['Points'] = df['Grade'].map(grade_mapping)\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "9f8a68aa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Grade Points\n",
"0 A 4\n",
"1 B 3\n",
"2 C 2\n",
"3 A 4\n",
"4 B 3\n"
]
}
],
"source": [
"# Function to apply to each value\n",
"def grade_to_points(grade):\n",
" if grade == 'A':\n",
" return 4\n",
" elif grade == 'B':\n",
" return 3\n",
" elif grade == 'C':\n",
" return 2\n",
" return 0\n",
"\n",
"df['Points'] = df['Grade'].map(grade_to_points)\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f516371f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Grade Points New_Points\n",
"0 A 4 5\n",
"1 B 3 4\n",
"2 C 2 3\n",
"3 A 4 5\n",
"4 B 3 4\n"
]
}
],
"source": [
"# Function to increase points by 1\n",
"df['New_Points'] = df['Points'].apply(lambda x: x + 1)\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c34f6b3f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Math Science Total\n",
"0 80 88 168\n",
"1 90 92 182\n",
"2 85 84 169\n"
]
}
],
"source": [
"# Example DataFrame\n",
"data = {'Math': [80, 90, 85], 'Science': [88, 92, 84]}\n",
"df = pd.DataFrame(data)\n",
"\n",
"# Function to calculate total marks for each row\n",
"df['Total'] = df.apply(lambda row: row['Math'] + row['Science'], axis=1)\n",
"print(df)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "903da672",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

You might also like