2zadanie/pandas.ipynb

410 lines
11 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3013204a-5530-4de5-9362-c91dcf217fe8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Техническая информация:\n",
"<class 'pandas.DataFrame'>\n",
"RangeIndex: 4 entries, 0 to 3\n",
"Data columns (total 3 columns):\n",
" # Column Non-Null Count Dtype\n",
"--- ------ -------------- -----\n",
" 0 Имя 4 non-null str \n",
" 1 Возраст 4 non-null int64\n",
" 2 Баллы 4 non-null int64\n",
"dtypes: int64(2), str(1)\n",
"memory usage: 228.0 bytes\n",
"None\n"
]
}
],
"source": [
"import pandas as pd\n",
"\n",
"data = {\n",
" \"Имя\": [\"Анна\", \"Борис\", \"Виктор\", \"Галина\"],\n",
" \"Возраст\": [21, 22, 23, 24],\n",
" \"Баллы\": [89, 76, 95, 82]\n",
"}\n",
"df = pd.DataFrame(data)\n",
"\n",
"print(\"Техническая информация:\")\n",
"print(df.info())\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b6287eb3-dcbe-4015-b86d-a4a8f42cbd03",
"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>Имя</th>\n",
" <th>Возраст</th>\n",
" <th>Баллы</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Анна</td>\n",
" <td>21</td>\n",
" <td>89</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Борис</td>\n",
" <td>22</td>\n",
" <td>76</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Виктор</td>\n",
" <td>23</td>\n",
" <td>95</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Галина</td>\n",
" <td>24</td>\n",
" <td>82</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Имя Возраст Баллы\n",
"0 Анна 21 89\n",
"1 Борис 22 76\n",
"2 Виктор 23 95\n",
"3 Галина 24 82"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d082ec60-d76b-4c2d-9471-ef6c9a11c6a7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1fd75bbf-d98d-4ef2-8541-2e6644ef26c2",
"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>Имя</th>\n",
" <th>Возраст</th>\n",
" <th>Баллы</th>\n",
" <th>Новый столбец</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>Анна</td>\n",
" <td>21</td>\n",
" <td>89</td>\n",
" <td>97.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Борис</td>\n",
" <td>22</td>\n",
" <td>76</td>\n",
" <td>83.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Виктор</td>\n",
" <td>23</td>\n",
" <td>95</td>\n",
" <td>104.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Галина</td>\n",
" <td>24</td>\n",
" <td>82</td>\n",
" <td>90.2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Имя Возраст Баллы Новый столбец\n",
"0 Анна 21 89 97.9\n",
"1 Борис 22 76 83.6\n",
"2 Виктор 23 95 104.5\n",
"3 Галина 24 82 90.2"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[\"Новый столбец\"] = df[\"Баллы\"] * 1.1\n",
"df # Выводим результат, чтобы проверить\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5768c86e-9f44-466f-a23d-41465cfa6807",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e7738859-8f4f-44dc-9afc-a0dcf195b7d2",
"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>Баллы</th>\n",
" </tr>\n",
" <tr>\n",
" <th>Имя</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Анна</th>\n",
" <td>89.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Борис</th>\n",
" <td>76.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Виктор</th>\n",
" <td>95.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Галина</th>\n",
" <td>82.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Баллы\n",
"Имя \n",
"Анна 89.0\n",
"Борис 76.0\n",
"Виктор 95.0\n",
"Галина 82.0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Посчитаем средний балл (в данном случае он будет равен самому баллу)\n",
"grouped = df.groupby(\"Имя\").agg({\"Баллы\": \"mean\"})\n",
"grouped\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b20b87a-4a84-4fc7-8ff8-73853ece191c",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 5,
"id": "155692d0-03dc-4802-b976-f422eb01b8c8",
"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>Имя</th>\n",
" <th>Возраст</th>\n",
" <th>Баллы</th>\n",
" <th>Новый столбец</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>Борис</td>\n",
" <td>22</td>\n",
" <td>76</td>\n",
" <td>83.6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>Виктор</td>\n",
" <td>23</td>\n",
" <td>95</td>\n",
" <td>104.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>Галина</td>\n",
" <td>24</td>\n",
" <td>82</td>\n",
" <td>90.2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Имя Возраст Баллы Новый столбец\n",
"1 Борис 22 76 83.6\n",
"2 Виктор 23 95 104.5\n",
"3 Галина 24 82 90.2"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filtered_df = df[df[\"Возраст\"] > 21]\n",
"filtered_df\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4a5a6b8-fa2e-448e-8584-577f9134c876",
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}