pandas基礎運算和合併示例

2021-10-03 07:31:35 字數 4463 閱讀 7202

,"source":[

"### python資料分析的三劍客"]}

,,"outputs":[

],"source":[

"import numpy as np\n"

,"\n"

,"import pandas as pd\n"

,"\n"

,"# pip install matplotlib\n"

,"# 畫圖,視覺化!\n"

,"# 頭號玩家,虛擬實境遊戲,視覺化,立體化\n"

,"import matplotlib.pyplot as plt"]}

,,"source":[

"### 生成物件"]}

,,"outputs":[

,"execution_count":3

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# 一維的\n"

,"s = pd.series(data = [88,103,68,134,99],index = ['張三','李四','王五','老路','jack'],\n"

," dtype=np.float32,name = 'python')\n"

,"s"]}

,,"outputs":[

,"execution_count":9

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"df = pd.dataframe(data = np.random.randint(0,150,size=(5,3)),\n"

," index = ['張三','李四','王五','老路','jack'],\n"

," columns=['python','en','數學'])\n"

,"df"]}

,,"source":[

"### 檢視資料"]}

,,"outputs":[

,"execution_count":11

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"s['王五']"]}

,,"outputs":[

,"execution_count":13

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"df.head(3)"]}

,,"outputs":[

,"execution_count":14

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"df.tail(3)"]}

,,"source":[

"### 選擇"]}

,,"outputs":[

],"source":[

"df['張三','王五']"]}

,,"outputs":[

,"execution_count":20

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# 列索引\n"

,"df[['python','en']]"]}

,,"outputs":[

,"execution_count":24

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"df[0::2]"]}

,,"outputs":[

,"execution_count":27

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# 檢索行索引\n"

,"df.loc[['張三','jack']]"]}

,,"outputs":[

,"execution_count":29

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# 檢索行,可以使用iloc\n"

,"# 帶有i,數字0,1,2,3\n"

,"df.iloc[[0,4]]"]}

,,"outputs":[

,"execution_count":30

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# 資料庫中,存著不同人的成績,需要獲取資料庫中,張三,jack的python和數學成績\n"

,"# ???sql,你在腦子,想一下\n"

,"# pandas比sql簡單。\n"

,"df[['python','數學']].loc[['張三','jack']]"]}

,,"source":[

"### 缺失值"]}

,,"outputs":[

,"execution_count":38

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# pandas中的缺失值,使用nan表示,nan:not a number\n"

,"cond = df >= 31\n"

,"df2 = df[cond]\n"

,"df2"]}

,,"outputs":[

,"execution_count":36

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# 填充空值,fillna呼叫,返回\n"

,"df2.fillna(60)"]}

,,"outputs":[

,"execution_count":39

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"df2"]}

,,"outputs":[

,"execution_count":40

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# dropna刪除空值\n"

,"df2.dropna()"]}

,,"outputs":[

,"execution_count":41

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"# axis 軸,座標:x軸,y軸\n"

,"# dataframe二維的:行(0),列(1)\n"

,"df2.dropna(axis = 0 )"]}

,,"outputs":[

,"execution_count":43

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"df2"]}

,,"outputs":[

,"execution_count":42

,"metadata":,

"output_type"

:"execute_result"}]

,"source":[

"df2.dropna(axis = 1)"]}

],"metadata":,

"language_info":,

"file_extension"

:".py"

,"mimetype"

:"text/x-python"

,"name"

:"python"

,"nbconvert_exporter"

:"python"

,"pygments_lexer"

:"ipython3"

,"version"

:"3.8.1"}}

,"nbformat":4

,"nbformat_minor":4

}

pandas基礎運算

python for data analysis pandas可以對不同索引的物件進行算術運算。例如 當物件相加時,如果存在不同的索引對,其結果的索引就是該索引對的並集。自動的資料對齊操作在不重疊的索引處引入了na值。並且會在算術運算中傳播。in 1 import numpy as np in 2 ...

C 的可空型別和合併運算子

c 提供了乙個特殊的資料型別,nullable型別,可空型別可以表示其基礎值型別正常範圍內的值,再加上乙個null值。在處理資料庫和其他包含可能未賦值的元素的資料型別時,將nul賦值給數值型別或布林型的功能特別有用。可以將bool型賦值未定義。宣告乙個nullable型別 null 例如 using...

Pandas基礎 使用者遊覽日誌時間合併排序

有乙份下面格式使用者遊覽日誌的資料 複製下面顯示的 後,執行下面的 才會出現相同的結果 import pandas as pd df pd.read clipboard df結果 uidstart end0a1 21a4 72a3 63a8 94b2 35b4 76b10 117b6 88b12 1...