1. 程式人生 > >18-09-26 pandas 基礎學習01

18-09-26 pandas 基礎學習01

# 一 資料結構介紹********************

# 在pandas 中有兩類非常重要的資料結構,1序列Series 和2 資料框DataFrame,
# Series 類似於numpy 中的一維資料,除了通吃一維陣列可用的函式和方法,而且其可通過
#索引標籤的方式獲取資料,還具有索引的自動對齊功能;
# DataFrame 類似於numpy 中的二維陣列,同樣可以通用numpy 陣列的函式和方法,二維還具有
# 其他靈活應用。

# 1 Series 的建立 類似一維陣列==============

# 序列的建立主要有三種方式
# 方式1 通過一維陣列建立序列

import numpy as np
import pandas as pd

arr1 = np.arange(10)
# print(arr1,type(arr1))
# [0 1 2 3 4 5 6 7 8 9] <class 'numpy.ndarray'>

s1 = pd.Series(arr1) #自動建立索引資料並自動補齊
# print(s1,type(s1))
# 0 0
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 6
# 7 7
# 8 8
# 9 9
# dtype: int32 <class 'pandas.core.series.Series'>

#自定義索引建立
# _s1 = pd.Series([2,4],index=["王","羅"])
_s1 = pd.Series({"王":2,"羅":4}) #一樣的
# print(_s1)
# 王 2
# 羅 4
# dtype: int64

# 方式2 通過字典的方式建立序列 (字典的鍵代表縱軸索引)


dic1 = {"a":1,"b":2,"c":3,"d":4,"e":5}
s2 = pd.Series(dic1)
# print(s2,type(s2))
# a 1
# b 2
# c 3
# d 4
# e 5
# dtype: int64 <class 'pandas.core.series.Series'>

# 方式3 通過DataFrame 中的某一行或某一列建立序列
# 暫時略

# 2 DataFrame =====的建立 類似二維陣列 帶有橫軸縱軸的索引##
# 資料框的建立主要有三種方式

# 方式1 通過二維陣列建立資料框(橫向 縱向加上類似索引資料)
arr2 = np.array(np.arange(12)).reshape(4,3)
# print(arr2,type(arr2))
# [[ 0 1 2]
# [ 3 4 5]
# [ 6 7 8]
# [ 9 10 11]] <class 'numpy.ndarray'>

df1 = pd.DataFrame(arr2)
# print(df1,type(df1))
# 0 1 2
# 0 0 1 2
# 1 3 4 5
# 2 6 7 8
# 3 9 10 11 <class 'pandas.core.frame.DataFrame'>

# 方式2 通過字典的方式建立資料框 (字典的鍵代表橫軸索引)

# 以下以兩種字典來建立資料框,
# 一個是字典列表,一個是巢狀字典

dic2 = {"a":[1,2,3,4],"b":[5,6,7,8],"c":[9,10,11,12],"d":[13,14,15,16]}

df2 = pd.DataFrame(dic2)
# print(df2,type(df2))

# a b c d
# 0 1 5 9 13
# 1 2 6 10 14
# 2 3 7 11 15
# 3 4 8 12 16 <class 'pandas.core.frame.DataFrame'>
# 一個是巢狀字典 外層字典的鍵對應橫軸的索引 ,內層字典的鍵對應縱軸的索引
dic3 = {"A":{"a1":1,"b1":2,"c1":3,"d1":4},
"B":{"a2":5,"b2":6,"c2":7,"d2":8},
"C":{"a3":9,"b3":10,"c3":11,"d3":12}}

df3 = pd.DataFrame(dic3)
# print(df3,type(df3))

# A B C
# a1 1.0 NaN NaN
# a2 NaN 5.0 NaN
# a3 NaN NaN 9.0
# b1 2.0 NaN NaN
# b2 NaN 6.0 NaN
# b3 NaN NaN 10.0
# c1 3.0 NaN NaN
# c2 NaN 7.0 NaN
# c3 NaN NaN 11.0
# d1 4.0 NaN NaN
# d2 NaN 8.0 NaN
# d3 NaN NaN 12.0 <class 'pandas.core.frame.DataFrame'>


# 方式三 通過資料框的方式建立資料框(列的標籤索引)


df4 = df3[["A","B"]] #擷取一部分的形式
# print(df4,type(df4))

# A B
# a1 1.0 NaN
# a2 NaN 5.0
# a3 NaN NaN
# b1 2.0 NaN
# b2 NaN 6.0
# b3 NaN NaN
# c1 3.0 NaN
# c2 NaN 7.0
# c3 NaN NaN
# d1 4.0 NaN
# d2 NaN 8.0
# d3 NaN NaN <class 'pandas.core.frame.DataFrame'>

df5 = df3["C"] #擷取一部分資料框
# print(df5,type(df5))
# a1 NaN
# a2 NaN
# a3 9.0
# b1 NaN
# b2 NaN
# b3 10.0
# c1 NaN
# c2 NaN
# c3 11.0
# d1 NaN
# d2 NaN
# d3 12.0
# Name: C, dtype: float64 <class 'pandas.core.series.Series'>

# 手動建立橫軸縱軸索引的資料框
a = pd.DataFrame([[2,4,6,8], [12,14,16,18]], index=['語文', '數學'], columns=['周', '孔', '王', '李'])
# print(a)

# 周 孔 王 李
# 語文 2 4 6 8
# 數學 12 14 16 18


# 二 資料索引index********************

# 細緻的朋友可能會發現一個現象,無論是序列也好,還是資料框也好,物件的最左邊總有一個非
# 原始資料物件,這個是什麼呢?不錯這就是我們接下來要介紹的索引

# 在我們看來序列或資料框的索引有兩大用處,一個數通過索引值或索引標籤獲取目標資料,
# 另一個是通過索引,可以是序列或資料框的計算,操作實現自動化對齊,下面我們具體應用下、

# 1 通過索引值或索引標籤獲Series(序列)取資料 ==============
s3 = pd.Series(np.array([1,1,2,3,5,8])) #序列自動生成一個從0開始的自增索引
# print(s3,type(s3))

# 0 1
# 1 1
# 2 2
# 3 3
# 4 5
# 5 8
# dtype: int32 <class 'pandas.core.series.Series'>

# 自定義新增索引
s3.index = ["a","b","c","d","e","f"] # 自定義更改索引值
# print(s3)
# a 1
# b 1
# c 2
# d 3
# e 5
# f 8
# dtype: int32
#序列Series 的切片簡單應用
# 序列有了索引,就可以通過索引值或者索引標籤進行資料的獲取
# print(s3[3]) #第四個索引值獲取的資料 值3
# print(s3["d"]) #索引d 這屬於標籤索引 是名稱 對應的值3
# print(s3[[1,3,5]]) # 通過陣列選取索引 對應的值

# b 1
# d 3
# f 8
# dtype: int32

# print(s3[["a","b","e","d"]])#多行的資料

# a 1
# b 1
# e 5
# d 3
# dtype: int32

# print(s3[:4]) #索引的值從0 到第3個 四個值資料

# a 1
# b 1
# c 2
# d 3
# dtype: int32

# print(s3["c":]) #從第c 個索引到最後
# c 2
# d 3
# e 5
# f 8
# dtype: int32

# print(s3["b":"e"])
# b 1
# c 2
# d 3
# e 5
# dtype: int32



# 注意 千萬注意----------------======¥¥¥¥¥
# 如果通過索引標籤獲取資料的話,末端標籤所對應的值是可以返回的,在一維陣列中就無法通過索引
# 標籤獲取資料,這也是序列與一維陣列的一個區別


# 2 自動化對齊 ============== 只要相同的索引值相加,不相同的為NaN
# 如果有兩個序列,需要對這兩個序列進行算數運算,這時索引的存在就體現了它的價值
# 自動化對齊

s4 = pd.Series(np.array([10,11,12,13,14,15]))
s4.index = ["a","b","c","d","e","f"]
# print(s4)
# a 10
# b 11
# c 12
# d 13
# e 14
# f 15
# dtype: int32

s5 = pd.Series(np.array([2,3,4,5,6,7]))
s5.index = ["a","c","g","b","d","f"]
# print(s5)
# a 2
# c 3
# g 4
# b 5
# d 6
# f 7
# dtype: int32

#序列之和
# print(s4 + s5) #自動補齊(只要相同的索引值相加,不相同的為NaM)
# a 12.0
# b 16.0
# c 15.0
# d 19.0
# e NaN
# f 22.0
# g NaN
# dtype: float64

#序列之間整除
# print(s4 //s5 ) #整除
# a 5.0
# b 2.0
# c 4.0
# d 2.0
# e NaN
# f 2.0
# g NaN
# dtype: float64


# 由於s5中沒有對應的g索引,s6中沒有對應的e索引,所以資料的運算會產生兩個缺失值NaN。
# 注意,這裡的算術結果就實現了兩個序列索引的自動對齊,而非簡單的將兩個序列加總或相除。
# 對於資料框的對齊,不僅僅是行索引的自動對齊,同時也會自動對齊列索引(變數名)
# 資料框中同樣有索引,而且資料框是二維陣列的推廣,所以其不僅有行索引,而且還存在列索引,
# 關於資料框中的索引相比於序列的應用要強大的多,這部分內容將放在資料查詢中講解。



#三 pandas 的應用********************

# 這裡的查詢資料相當於R語言裡的subset功能,可以通過布林索引有針對的
# 選取元資料的子集、指定行,指定列等,我們先匯入一個students資料集

可以手動建立縱向索引 index 也可以建立橫向列的colums

# 1 手動建立橫軸縱軸索引的資料框
df6 = pd.DataFrame([[2,4,6,8], [12,14,16,18]], index=['語文', '數學'], columns=['周', '孔', '王', '李'])
# print(df6)

# 周 孔 王 李
# 語文 2 4 6 8
# 數學 12 14 16 18
df7 = pd.DataFrame([["小馬","male",12,156,40,"上海",79],["小驢","male",14,166,50,"深圳",89]],index=[1,2],
columns=["name","sex","age","heigh","weight","address","grade"])
# print(df7)
# name sex age heigh weight address grade
# 1 小馬 male 12 156 40 上海 79
# 2 小驢 male 14 166 50 深圳 89

# 2指定列排順序 並可擷取不同的列 方式一擷取列 其實這種也行[:,[0,3]] (擷取第1列和第4列)
df8 = pd.DataFrame(df7,columns=["name","age","sex"])
# print(df8)
# name age sex
# 1 小馬 12 male
# 2 小驢 14 male
# 2-1 方式二擷取列
df9 = df7[["name","age","sex"]]
# print(df9)

# 3 傳入列在資料中找不到就會產生NA值
df10 = pd.DataFrame(df7,index=[1,2,3,4,5],columns=["name","age","sex","address"])
# print(df10)
# name age sex address
# 1 小馬 12.0 male 上海
# 2 小驢 14.0 male 深圳
# 3 NaN NaN NaN NaN
# 4 NaN NaN NaN NaN
# 5 NaN NaN NaN NaN

# 4 巢狀字典(字典組成的字典)建立DataFrame ,不等長也可,自動填充NaN 外層鍵是列索引,內行鍵是行索引

dic4 = {
'name':{1:'小明',2:'小華',3:'小紅',4:'小青',5:'小蘭'},
'sex':{1:1,2:0,3:0,4:1,5:0},
'age':{2:28,3:38,4:48,5:8} #此時缺少索引1
}

df11 = pd.DataFrame(dic4)
# print(df11)

# name sex age
# 1 小明 1 NaN
# 2 小華 0 28.0
# 3 小紅 0 38.0
# 4 小青 1 48.0
# 5 小蘭 0 8.0

可擷取不同的行 類似擷取不同的行 對比擷取不同的列 columns=["name","age","sex"]) ============
df12 = pd.DataFrame(df11,index=[2,3,4,5,6])  
# print(df12)
# name sex age
# 2 小華 0.0 28.0
# 3 小紅 0.0 38.0
# 4 小青 1.0 48.0
# 5 小蘭 0.0 8.0
# 6 NaN NaN NaN

# ---
# 5 和Series 組成的字典,建立DataFrame同巢狀字典,指定內層字典鍵(行索引),沒有的自動填充NaM

dic5 = {
"name":pd.Series(["小王","小羅","小張","小李","小劉"],
index=[1,2,3,4,5]),
"sex":pd.Series([1,1,1,0,0], index=[1,2,3,4,5]),
"age":pd.Series([20,31,33,22],index=[2,3,4,5])
}
df12 = pd.DataFrame(dic5)
# print(df12)
# name sex age
# 1 小王 1 NaN
# 2 小羅 1 20.0
# 3 小張 1 31.0
# 4 小李 0 33.0
# 5 小劉 0 22.0

# 6刪除行預設 axis=0 刪除列 axis=1
df13 = df12.drop([3],axis=0)#刪除第三行按照標籤索引查詢
# print("刪除行",df13)
# name sex age
# 1 小王 1 NaN
# 2 小羅 1 20.0
# 4 小李 0 33.0
# 5 小劉 0 22.0

df14 = df12.drop(["sex"],axis=1) #刪除列
# print("刪除列",df14)
# 刪除行 name age
# 1 小王 NaN
# 2 小羅 20.0
# 3 小張 31.0
# 4 小李 33.0
# 5 小劉 22.0

# 7 遍歷DataFrame #每一行的資料加上 列標題
# for index, row in df12.iterrows():
# print("index:",index)
# print("row:",row)
# row: name 小王
# sex 1
# age NaN
# Name: 1, dtype: object
# row: name 小羅
# sex 1
# age 20
# Name: 2, dtype: object
# row: name 小張
# sex 1
# age 31
# Name: 3, dtype: object
# row: name 小李
# sex 0
# age 33
# Name: 4, dtype: object
# row: name 小劉
# sex 0
# age 22
# Name: 5, dtype: object

# 8 檢視相關 維度 型別 值 單值

# print("檢視維度:",df12.shape) #檢視維度: (5, 3)

# print("檢視型別:",df12.dtypes)
# 檢視型別: name object
# sex int64
# age float64
# dtype: object

# print("檢視值:",df12.values)
# 檢視值: [['小王' 1 nan]
# ['小羅' 1 20.0]
# ['小張' 1 31.0]
# ['小李' 0 33.0]
# ['小劉' 0 22.0]]

# print("檢視單值:",df12["sex"][3]) # 檢視單值: 1

# print(df12.head(2)) #顯示頭部2行,但是預設是5行
# name sex age
# 1 小王 1 NaN
# 2 小羅 1 20.0

# print(df12.tail(3)) #顯示末尾3行,預設是5行
# name sex age
# 3 小張 1 31.0
# 4 小李 0 33.0
# 5 小劉 0 22.0

# print(df12.info()) #相關資訊概況,行數,列數,列索引,列非空值個數,列型別,行型別,記憶體佔用
# <class 'pandas.core.frame.DataFrame'>
# Int64Index: 5 entries, 1 to 5
# Data columns (total 3 columns):
# name 5 non-null object
# sex 5 non-null int64
# age 4 non-null float64
# dtypes: float64(1), int64(1), object(1)
# memory usage: 320.0+ bytes
# None

# print(df12.describe()) #快速綜合統計結果,計數,均值,標準差,最大值,四分位數,最小值
# sex age
# count 5.000000 4.000000
# mean 0.600000 26.500000 #????
# std 0.547723 6.454972 #????
# min 0.000000 20.000000
# 25% 0.000000 21.500000
# 50% 1.000000 26.500000
# 75% 1.000000 31.500000
# max 1.000000 33.000000

# 四 索引查詢 ********************

# 用於連續或不連續(行列有間隔)行列區塊查詢
# 解決了DataFrame 進行行標籤查詢的問題
# 兩種查詢方式:
# df.loc[行,列] 標籤索引 自定義索引
# df.iloc[行,列] 位置索引,預設索引

# 引數書寫順序都是先行後列

dic5 = {
"name":pd.Series(["小王","小羅","小張","小李","小劉"],
index=[1,2,3,4,5]),
"sex":pd.Series([1,1,1,0,0], index=[1,2,3,4,5]),
"age":pd.Series([20,31,33,22],index=[2,3,4,5])
}
df15 = pd.DataFrame(dic5)

# 1 查詢單行 df.loc[標籤索引] ()
# print("標籤單行索引",df15.loc[3])
# 標籤單行索引 name 小張
# sex 1
# age 31
# Name: 3, dtype: object

# df15.iloc[位置索引]

# print("位置索引單行",df15.iloc[3])#索引第四個值
# 位置索引單行 name 小李
# sex 0
# age 33
# Name: 4, dtype: object

# 2 查詢單列 前面機上冒號
# df.loc[:,"列名"] #標籤索引
# df.iloc[:,索引數字] #位置索引

# print("標籤索引單列name",df15.loc[:,"name"])
# 標籤索引單列name 1 小王
# 2 小羅
# 3 小張
# 4 小李
# 5 小劉
# Name: name, dtype: object

# print("位置索引單列name",df15.iloc[:,0])
# 位置索引單列name 1 小王
# 2 小羅
# 3 小張
# 4 小李
# 5 小劉
# Name: name, dtype: object

# 3查詢多行,雙中括號
# print(df15.loc[[2,5]]) #讀取第二行 和第五行
# name sex age
# 2 小羅 1 20.0
# 5 小劉 0 22.0

# print(df15.iloc[[2,4]]) #讀取索引為2 和索引為4 對應的資料
# 3 小張 1 31.0
# 5 小劉 0 22.0

# 4 查詢多列
# print(df15.loc[:,["name","age"]]) #標籤索引
# name age
# 1 小王 NaN
# 2 小羅 20.0
# 3 小張 31.0
# 4 小李 33.0
# 5 小劉 22.0

# print(df15.iloc[:,[0,2]]) #位置索引
# name age
# 1 小王 NaN
# 2 小羅 20.0
# 3 小張 31.0
# 4 小李 33.0
# 5 小劉 22.0

# 5 查詢單個單元格方法 [行,列]
# print(df15.loc[1,"name"]) #標籤索引 第一行 name列
# 小王
# print(df15.at[1,"name"]) #標籤索引 速度和效率會更高at 用法
# 小王
# 以下是位置索引
# print(df15.iloc[0,0])# 位置索引
# 小王


# 6 查詢多個單元格

# 6-1 均是標籤索引 就是寫上標籤的名字即可
# 一行多列Seires
# print(df15.loc[1,["name","sex"]]) # 標籤索引
# name 小王
# sex 1
# Name: 1, dtype: object

# 多行一列
# print(df15.loc[[1,4],"sex"])#第一行和第四行 的sex 列
# 1 1
# 4 0
# Name: sex, dtype: int64

# 多行多列
# print(df15.loc[[1,3],["sex","age"]]) #標籤索引
# sex age
# 1 1 NaN
# 3 1 31.0


# 6-2 查詢多表格 是位置索引或是標籤索引

# print(df15.iloc[0,[0,2]]) #第一行第一個資料 和一行和第3個數據
# name 小王
# age NaN
# Name: 1, dtype: object

# print(df15.iloc[[0,1],2]) #第一行和第二行,和第三宗地的結合部分資料
# 1 NaN
# 2 20.0
# Name: age, dtype: float64

# print(df15.iloc[[0,1],[0,2]]) #多行多列資料 按照索引第一行第二行 對應第一 第三列資料
# name age
# 1 小王 NaN
# 2 小羅 20.0

# 五 切片查詢 ******************** 擷取行 (冒號):在最後,擷取列 (冒號):在最前

# 用於連續行列區塊查詢
# 比索引查詢簡單方便,單不能查詢非連續行列區塊
# 單個或不連續行列區塊使用縮影查詢,連續行列區塊使用切片查詢

# 切片查詢的區間
# iloc 預設是位置索引是從左到右開區間(左包右不包)
# loc 自定義標籤索引是從左到右閉區間(左包右也包)
# 因為標籤索引不包含位置索引,使用時很難知道索引前後是什麼
dic5 = {
"name":pd.Series(["小王","小羅","小張","小李","小劉"],
index=[1,2,3,4,5]),
"sex":pd.Series([1,1,1,0,0], index=[1,2,3,4,5]),
"age":pd.Series([20,31,33,22],index=[2,3,4,5])
}
df16 = pd.DataFrame(dic5)
# print(df16)
# name sex age
# 1 小王 1 NaN
# 2 小羅 1 20.0
# 3 小張 1 31.0
# 4 小李 0 33.0
# 5 小劉 0 22.0

# 1 選取一行
# print(df16.loc[2,:]) #標籤索引 讀取第二行
# print(df16.loc[2])

# print(df16.iloc[1,:])#位置索引 讀取第二行
# print(df16.iloc[1])

# name 小羅
# sex 1
# age 20
# Name: 2, dtype: object


# 2 選取一列
# print(df16.loc[:,"name"]) #標籤索引方式
# print(df16.iloc[:,0]) #位置索引方式

# 1 小王
# 2 小羅
# 3 小張
# 4 小李
# 5 小劉
# Name: name, dtype: object

# 3 切片選取連續多行 ???暫時有點不理解(自己理解是iloc 位置索引如果是3 包括前3個 0 1 2 )
# print(df16.loc[:3:]) #左右全包從開始到3
# print(df16.iloc[:3,:]) #從開始的0 索引到2 共3個

# name sex age
# 1   小王    1   NaN
# 2 小羅 1 20.0
# 3 小張 1 31.0

# 4 切片選取連續多列
# print(df16.loc[:,"sex":"age"]) #注意是個冒號 標籤名:標籤名
# print(df16.iloc[:,1:3]) #列索引:列索引

# 5 切片選取多行多列的聚合
# print(df16.loc[2:4,"sex":"age"])
# print(df16.iloc[1:4,1:3]) #此時的4 是索引4 並不是標籤4


# 六 過濾查詢 ********************

# 通過列布爾值過濾、篩選查詢 ,不通過索引而是通過值查詢
# 用於結果索引不確定的查詢
# 通過運算所得布林值對查詢結果進行過濾,
# 通過某列值過濾資料,返回布林值

# 1 選取年齡大於30的 一列資料
# print(df16["age"]>=30)
# print(df16.loc[:,"age"]>=30)#(列的標籤索引)age 這一列大於30
# 1 False
# 2 False
# 3 True
# 4 True
# 5 False
# Name: age, dtype: bool

# 2 布林值做 DataFrame 引數,返回DataFrame物件

# print(df16[df16["age"]>=30])
# print(df16[df16.loc[:,"age"]>=30])#
# name sex age
# 3 小張 1 31.0
# 4 小李 0 33.0

# 3 布林索引和切片結合 資料框["列名標籤"]
# print(df16.loc[df16["age"]>30,"name":"age"]) #或者 name":])
# name sex age
# 3 小張 1 31.0
# 4 小李 0 33.0

# 4 多重條件過濾,邏輯運算
# & 且的意思;
# | 或 的意思;
# - 非的意思(或者用!= 判斷)

# print((df16["age"] > 30) &(df16["sex"] == 1))
# print(df16[(df16["age"] > 30) &(df16["sex"] == 1)])
# name sex age
# 3 小張 1 31.0

# 5 where 過濾
# 5-1方式1 過濾 方法 通過布林值判斷過濾 去除NaN 的值
df17 = df16[["name","sex","age"]] #多列資料必須多箇中括號
# print(df17)
dfbool = df17>=1 #返回DataFrame 型別的布林值
# print(dfbool) #返回DataFrame 型別的布林值
# name sex age
# 1 True True False
# 2 True True True
# 3 True True True
# 4 True False True
# 5 True False True
dNan = df17[dfbool] #當引數傳來進來
# print(dNan)
# name sex age
# 1 小王 1.0 NaN
# 2 小羅 1.0 20.0
# 3 小張 1.0 31.0
# 4 小李 NaN 33.0
# 5 小劉 NaN 22.0

# 去掉含有缺失值的行
filter_NaN= dNan.dropna(how="any")
# print(filter_NaN)
# name sex age
# 2 小羅 1.0 20.0
# 3 小張 1.0 31.0

# 5-2 方式2 過濾方法還可以通過isin() 函式過濾取資料
NaN_bool = df16["sex"].isin([1]) #這個1是性別的識別符號
# print(filter_bool)
# 1 True
# 2 True
# 3 True
# 4 False
# 5 False
# Name: sex, dtype: bool

df18= df16[NaN_bool]
# print(df18)
# name sex age
# 1 小王 1 NaN
# 2 小羅 1 20.0
# 3 小張 1 31.0
# 通過布林值判斷過濾 去除NaN 的值
filter_NaN1 = df18.dropna(how="any")
# print("去除NaN的資料",filter_NaN)
# 去除NaN的資料 name sex age
# 2 小羅 1.0 20.0
# 3 小張 1.0 31.0

# 七 修改 update ********************

# 凡是通過查詢得到的值,直接賦值就可修改;
# 檢視和副本:通過索引和切片返回的資料是原資料的檢視,不是副本,任何對檢視的修改全部會反應到源資料上,
# 想獨立修改資料可以通過copy() 複製源資料的副本
# 類 list/ndarry 查詢方式修改

# 1 增加或修改一列的資料
_df16 = df16.copy()
# _df16["heigh"] = [150,160,170,180,190] #增加了一列 heigh
# print(_df16)
# name sex age heigh
# 1 小王 1 NaN 150
# 2 小羅 1 20.0 160
# 3 小張 1 31.0 170
# 4 小李 0 33.0 180
# 5 小劉 0 22.0 190

_df16["heigh"]=170 #全變成170
# print(_df16)
# name sex age heigh
# 1 小王 1 NaN 170
# 2 小羅 1 20.0 170
# 3 小張 1 31.0 170
# 4 小李 0 33.0 170
# 5 小劉 0 22.0 170

# 2索引或切片查詢方式修改
df19 = df16.loc[[1,3,5],["name","age"]] #標籤索引 三行 兩列 檢視資料
# print(df19)
# name age
# 1 小王 NaN
# 3 小張 31.0
# 5 小劉 22.0

2-1 修改整個三行 2列的資料,注意不能有兩個等號 不能再賦值變數
df16.loc[[1,3,5],["name","age"]]=[["wang",11],["zhang",22],["liu",33]] #1  3  5 三行,name age 兩列
# _df19 = df16.loc[[1,3],["name","age"]]=[["wang",11],["zhang",22],["liu",33]] # 這樣的話維度不夠三行的資料 只有2行
# print(df16) #???????????????????????????應該不是一下這種型別 個人以為
# name sex age
# 1 wang 1 11.0
# 2 小羅 1 20.0
# 3 zhang 1 22.0
# 4 小李 0 33.0
# 5 liu 0 33.0
 
# 3 通過where 過濾修改
df20 = df16[df16>=1]
# print("s",df20)
# s name sex age
# 1 小王 1.0 NaN
# 2 小羅 1.0 20.0
# 3 小張 1.0 31.0
# 4 小李 NaN 33.0
# 5 小劉 NaN 22.0
# df21 = df16[df16>=1] = 2 #不能再混合資料勒種中修改非NaN 值
# print(df21) 報錯 了

# 4 提取唯一資料型別資料(拷貝一下)

df16_2 = df16.loc[:,["name","age"]] #擷取2列資料
df16_cp = df16_2.copy()
# print("df16_2",df16_2)

# df16_2 name age
# 1 小王 NaN
# 2 小羅 20.0
# 3 小張 31.0
# 4 小李 33.0
# 5 小劉 22.0

# print(df16_2>25)
# name age
# 1 True False
# 2 True False
# 3 True True
# 4 True True
# 5 True False

# print(df16_2[df16_2>25])
# name age
# 1 小王 NaN
# 2 小羅 NaN
# 3 小張 31.0
# 4 小李 33.0
# 5 小劉 NaN
# ???????????????????????????????
# df16_2[df16_2>25] = 88
# print(df16_2) #報錯 TypeError:不能在非np類混合型別上進行布林設定。nan值


# 八刪除 Delete ********************

s6 = pd.Series(["wang","luo"],index=[2,3])
# print(s6)
# 2 wang
# 3 luo
# dtype: object

# 1 Series 物件的刪除

s7 = s6.drop(2) #刪除一行
# print(s7)
# 3 luo
# dtype: object

s8 = s6.drop([2,3]) ## 刪除多行
# print(s8)
# Series([], dtype: object)

df21 = pd.DataFrame({"name":["wang","luo","zang"],"sex":[1,0,1],"age":[11,22,33]})
# print(df21)
# name sex age
# 0 wang 1 11
# 1 luo 0 22
# 2 zang 1 33

# 2 DataFrame 物件刪除

# 2-1 刪除單行和多行
# 預設是刪除行,預設只改動檢視

# df22 = df21.drop(0) #刪除單行
# print(df22)
# name sex age
# 1 luo 0 22
# 2 zang 1 33

# df23 = df21.drop([0,2]) #刪除多行
# print(df23)
# name sex age
# 1 luo 0 22

# 2-2 刪除單列或者多列

# axis=1 刪除列,預設axis=0刪除行
# print(df21.drop("name",axis=1)) #s刪除單列
# sex age
# 0 1 11
# 1 0 22
# 2 1 33

# print(df21.drop(["age","sex"],axis=1)) #刪除多列
# name
# 0 wang
# 1 luo
# 2 zang

# 2-3 inplace=True,改動原資料,預設 inplace=False,只改動檢視?????????
# print(df21.drop(["age","sex"],axis=1,inplace=False)) #設定為True 結果為None
# name
# 0 wang
# 1 luo
# 2 zang

# 2-4 DataFrame 物件的索引命名

df21.index.name = "row"
df21.columns.name = "col"
# print(df21)
# col name sex age
# row
# 0 wang 1 11
# 1 luo 0 22
# 2 zang 1 33