## pandas简介
Pandas中一共有三种数据结构,分别为:Series、Dataframe和MultiIndex。
Python Data Analysis Library,面板数据(panel data)和python数据分析(data analysis)。最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数据包开发的PyData开发team继续开发和维护,属于PyData项目的一部分。Pandas最初被作为金融数据分析工具而开发出来,因此,pandas为时间序列分析提供了很好的支持。
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "9f7e44d2",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pdn",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "23c5a15f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 0.959924n",
"1 0.057490n",
"2 0.395029n",
"3 0.861239n",
"4 0.332671n",
"dtype: float64n",
"0 0.959924n",
"1 0.057490n",
"2 0.395029n",
"3 0.861239n",
"dtype: float64n",
"0 0.959924n",
"1 0.057490n",
"2 0.395029n",
"3 0.861239n",
"dtype: float64n",
"0 0.959924n",
"2 0.395029n",
"4 0.332671n",
"dtype: float64n",
"0 20.000000n",
"1 20.000000n",
"2 20.000000n",
"3 0.861239n",
"4 0.332671n",
"dtype: float64n"
]
}
],
"source": [
"#切片索引n",
"s=pd.Series(np.random.rand(5))n",
"print(s)n",
"print(s[0:4])n",
"print(s[:-1])n",
"print(s[::2])n",
"#修改值n",
"s[:-2]=20n",
"print(s)n"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "949b7461",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 64.755105n",
"1 50.714969n",
"2 52.834138n",
"3 89.628520n",
"4 69.999119n",
"dtype: float64n",
"##############################n",
"0 64.755105n",
"1 50.714969n",
"2 52.834138n",
"3 89.628520n",
"4 NaNn",
"dtype: float64n",
"##############################n",
"0 Truen",
"1 Falsen",
"2 Falsen",
"3 Truen",
"4 Falsen",
"dtype: bool
"##############################n",
"1 50.714969n",
"2 52.834138n",
"dtype: float64n",
"##############################n",
"0 Falsen",
"1 Falsen",
"2 Falsen",
"3 Falsen",
"4 Truen",
"dtype: booln",
"##############################n",
"0 Truen",
"1 Truen",
"2 Truen",
"3 Truen",
"4 Falsen",
"dtype: booln",
"##############################n",
"4 NaNn",
"dtype: float64n"
]
}
],
"source": [
"#布尔索引n",
"np.random.seed(88)n",
"s=pd.Series(np.random.rand(5)*100)n",
"print(s)n",
"print('#'*30)n",
"s[4]=Nonen",
"print(s)n",
"print('#'*30)n",
"bol=s>55n",
"print(bol,type(bol))n",
"print('#'*30)n",
"#通过布尔series获取值n",
"print(s[s<55])n",
"print('#'*30)n",
"#查看series的方法n",
"bol2=s.isnull()n",
"print(bol2)n",
"'''返回false没有缺失值返回true有缺失值 s.nonull()函数则相反'''n",
"print('#'*30)n",
"bol3=s.notnull()n",
"print(bol3)n",
"print('#'*30)n",
"#获取空值n",
"print(s[bol2])"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "6791fc89",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 64.755105n",
"1 50.714969n",
"2 52.834138n",
"3 89.628520n",
"4 69.999119n",
"5 71.429710n",
"6 71.733838n",
"7 22.281946n",
"8 17.515452n",
"9 45.684149n",
"dtype: float64n",
"##############################n",
"0 64.755105n",
"dtype: float64n",
"9 45.684149n",
"dtype: float64n"
]
}
],
"source": [
"#pandas数据结构series技巧----数据查看,重新索引,对齐,增,删,改n",
"#数据查看n",
"np.random.seed(88)n",
"s=pd.Series(np.random.rand(10)*100)n",
"print(s)n",
"print('#'*30)n",
"'''head,tail方法'''n",
"print(s.head(1))n",
"print(s.tail(1))n",
"n"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "c97af1c5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 64.755105n",
"1 50.714969n",
"2 52.834138n",
"3 89.628520n",
"4 69.999119n",
"dtype: float64n",
"##############################n",
"c NaNn",
"d NaNn",
"a NaNn",
"e NaNn",
"f NaNn",
"dtype: float64n",
"c 11.000000n",
"d 11.000000n",
"a 11.000000n",
"e 11.000000n",
"f 11.000000n",
"0 64.755105n",
"1 50.714969n",
"2 52.834138n",
"3 89.628520n",
"4 69.999119n",
"5 11.000000n",
"dtype: float64n"
]
}
],
"source": [
"#重新索引n",
"np.random.seed(88)n",
"s=pd.Series(np.random.rand(5)*100)n",
"print(s)n",
"print('#'*30)n",
"'''重新索引后面value为null'''n",
"s1=s.reindex(['c','d','a','e','f'])n",
"print(s1)n",
"s2=s.reindex(['c','d','a','e','f',0,1,2,3,4,5],fill_value=11)n",
"print(s2)"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "43356734",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a 64.755105n",
"b 50.714969n",
"c 52.834138n",
"dtype: float64n",
"##############################n",
"a 89.628520n",
"e 69.999119n",
"f 71.429710n",
"dtype: float64n",
"##############################n",
"a 154.383625n",
"b NaNn",
"c NaNn",
"e NaNn",
"f NaNn",
"dtype: float64n"
]
}
],
"source": [
"#对齐n",
"np.random.seed(88)n",
"s1=pd.Series(np.random.rand(3)*100,index=['a','b','c'])n",
"s2=pd.Series(np.random.rand(3)*100,index=['a','e','f'])n",
"print(s1)n",
"print('#'*30)n",
"print(s2)n",
"print('#'*30)n",
"print(s1+s2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "faa8a408",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e32f916a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d54af1d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5fcdd26",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "28345851",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "d1ff6b29",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb30e137",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "51ac6517",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa8f3c3a",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5e09818",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "8fdfb24a",
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}



