Python-100-Days/Day66-70/code/Day68.ipynb

425 lines
8.1 KiB
Plaintext
Raw Normal View History

2021-03-08 00:25:04 +08:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"%config InlineBackend.figure_format='svg'"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"plt.rcParams['font.sans-serif'] = 'FZJKai-Z03S'\n",
"plt.rcParams['axes.unicode_minus'] = False"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"一季度 320\n",
"二季度 180\n",
"三季度 300\n",
"四季度 405\n",
"dtype: int64"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ser1 = pd.Series(data=[320, 180, 300, 405], index=['一季度', '二季度', '三季度', '四季度'])\n",
"ser1"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"一季度 320\n",
"二季度 180\n",
"三季度 300\n",
"四季度 405\n",
"dtype: int64"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ser2 = pd.Series({'一季度': 320, '二季度': 180, '三季度': 300, '四季度': 405})\n",
"ser2"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"320 300 405\n",
"一季度 350\n",
"二季度 180\n",
"三季度 300\n",
"四季度 360\n",
"dtype: int64\n"
]
}
],
"source": [
"print(ser2[0], ser2[2], ser2[-1])\n",
"ser2[0], ser2[-1] = 350, 360 \n",
"print(ser2)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"350 300\n",
"一季度 380\n",
"二季度 180\n",
"三季度 300\n",
"四季度 360\n",
"dtype: int64\n"
]
}
],
"source": [
"print(ser2['一季度'], ser2['三季度'])\n",
"ser2['一季度'] = 380\n",
"print(ser2)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"二季度 180\n",
"三季度 300\n",
"dtype: int64\n",
"二季度 180\n",
"三季度 300\n",
"四季度 360\n",
"dtype: int64\n",
"一季度 380\n",
"二季度 400\n",
"三季度 500\n",
"四季度 360\n",
"dtype: int64\n"
]
}
],
"source": [
"print(ser2[1:3])\n",
"print(ser2['二季度': '四季度'])\n",
"ser2[1:3] = 400, 500\n",
"print(ser2)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"二季度 400\n",
"四季度 360\n",
"dtype: int64\n",
"一季度 380\n",
"二季度 500\n",
"三季度 500\n",
"四季度 520\n",
"dtype: int64\n"
]
}
],
"source": [
"print(ser2[['二季度', '四季度']])\n",
"ser2[['二季度', '四季度']] = 500, 520\n",
"print(ser2)"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"二季度 500\n",
"三季度 500\n",
"四季度 520\n",
"dtype: int64\n"
]
}
],
"source": [
"print(ser2[ser2 >= 500])"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1900\n",
"475.0\n",
"520\n",
"380\n",
"4\n",
"64.03124237432849\n",
"4100.0\n",
"500.0\n"
]
}
],
"source": [
"# 求和\n",
"print(ser2.sum())\n",
"# 求均值\n",
"print(ser2.mean())\n",
"# 求最大\n",
"print(ser2.max())\n",
"# 求最小\n",
"print(ser2.min())\n",
"# 计数\n",
"print(ser2.count())\n",
"# 求标准差\n",
"print(ser2.std())\n",
"# 求方差\n",
"print(ser2.var())\n",
"# 求中位数\n",
"print(ser2.median())"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"count 4.000000\n",
"mean 475.000000\n",
"std 64.031242\n",
"min 380.000000\n",
"25% 470.000000\n",
"50% 500.000000\n",
"75% 505.000000\n",
"max 520.000000\n",
"dtype: float64"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ser2.describe()"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"apple 3\n",
"pitaya 2\n",
"durian 1\n",
"banana 1\n",
"dtype: int64"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ser3 = pd.Series(data=['apple', 'banana', 'apple', 'pitaya', 'apple', 'pitaya', 'durian'])\n",
"ser3.value_counts()"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 10.0\n",
"1 20.0\n",
"3 30.0\n",
"dtype: float64"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ser4 = pd.Series(data=[10, 20, np.NaN, 30, np.NaN])\n",
"ser4.dropna()"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 10.0\n",
"1 20.0\n",
"2 40.0\n",
"3 30.0\n",
"4 40.0\n",
"dtype: float64"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ser4.fillna(value=40)"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 10.0\n",
"1 20.0\n",
"2 20.0\n",
"3 30.0\n",
"4 30.0\n",
"dtype: float64"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ser4.fillna(method='ffill')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.7"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}