Python3:Pandas的簡單使用2(針對DataFrame的操作:索引,修改和排序)

1.聲明

下面的內容主要針對與當前的DataFrame進行的操作,涉及到對DataFrame的索引操作,賦值,修改還有排序等操作,主要用於本人複習

2.DataFrame的索引和修改操作

1.由於前面在創建DataFrame的時候,產生的數據是具有行列索引的,所以可對當前的index和columns進行相應的操作,這些操作可能涉及到其他操作

# 對當前的DataFrame進行索引的操作
import numpy as np
import pandas as pd

df = pd.DataFrame({
    "1th": [10, 11, 12, 13, 14],
    "2th": [20, 21, 22, 23, 24],
    "3th": [30, 31, 32, 33, 34],
    "4th": [40, 41, 42, 43, 44],
    "5th": [50, 51, 52, 53, 54],
})
print("輸出當前的數據:\n{}".format(df))
# 重置當前的索引,使用當前的reset_index返回的是當前數據重置索引的副本
print("輸出當前重置索引的數據:\n{}".format(df.reset_index()))

print("輸出設置了索引後的數據:\n{}".format(df.set_index("1th", drop=False))) # 發現當前的1th這一行的數據變成當前的索引列
print("輸出設置了索引後的數據:\n{}".format(df.set_index("1th", drop=True)))
print("輸出當前的數據:\n{}".format(df))

# 爲當前的數據添加多個索引
pd_array = df.set_index(["1th","2th"])
# 發現當前的索引變成多索引了:MultiIndex類型的數據,並且具有名稱names
print(pd_array.index)

# MultiIndex 
print("輸出當前的MultiIndex中的name屬性:\n{}".format(pd_array.index.names))
print("輸出當前的MultiIndex中的levels屬性:\n{}".format(pd_array.index.levels))

結果:

輸出當前的數據:
1th 2th 3th 4th 5th
0 10 20 30 40 50
1 11 21 31 41 51
2 12 22 32 42 52
3 13 23 33 43 53
4 14 24 34 44 54
輸出當前重置索引的數據:
index 1th 2th 3th 4th 5th
0 0 10 20 30 40 50
1 1 11 21 31 41 51
2 2 12 22 32 42 52
3 3 13 23 33 43 53
4 4 14 24 34 44 54
輸出設置了索引後的數據:
1th 2th 3th 4th 5th
1th
10 10 20 30 40 50
11 11 21 31 41 51
12 12 22 32 42 52
13 13 23 33 43 53
14 14 24 34 44 54
輸出設置了索引後的數據:
2th 3th 4th 5th
1th
10 20 30 40 50
11 21 31 41 51
12 22 32 42 52
13 23 33 43 53
14 24 34 44 54
輸出當前的數據:
1th 2th 3th 4th 5th
0 10 20 30 40 50
1 11 21 31 41 51
2 12 22 32 42 52
3 13 23 33 43 53
4 14 24 34 44 54
MultiIndex([(10, 20),
(11, 21),
(12, 22),
(13, 23),
(14, 24)],
names=[‘1th’, ‘2th’])
輸出當前的MultiIndex中的name屬性:
[‘1th’, ‘2th’]
輸出當前的MultiIndex中的levels屬性:
[[10, 11, 12, 13, 14], [20, 21, 22, 23, 24]]

1.我們可以使用reset_index()方式讓前面設置的索引失效

2.可以使用set_index()指定哪個作爲索引,drop是否爲True或者False表示是否保留之前的索引

3.當我們設置了set_index多個索引的時候,就相當於從多個維度去檢索數據,相當於Panel了

4.可以通過pd.index.XXX方式獲取和索引(行)相關的數據信息

3.DataFrame的排序和賦值操作

# 賦值操作和排序操作
import numpy as np
import pandas as pd

np_array = np.random.randint(1, 10, (5, 5))
pd_array = pd.DataFrame(np_array, index=["行{}".format(i) for i in range(5)],
                        columns=["列{}".format(i) for i in range(5)])
print("當前生成的數據爲:\n{}".format(pd_array))

# 改變當前列1中的所有數據並設置爲1
pd_array["列1"] = 1
print("輸出修改後的數據:\n{}".format(pd_array))

# 改變列2的數據爲1,2,3,4,5
pd_array["列3"] = [1, 2, 3, 4, 5]
print("輸出修改後的數據:\n{}".format(pd_array))

# 修改行0中的所有的數據爲 0
pd_array[:1] = 0
print("輸出修改後的數據:\n{}".format(pd_array))

# 使用loc的方式修改行1中的所有數據爲 1,2,3,4,5
pd_array.loc["行1"] = [1, 2, 3, 4, 5]
print("使用loc的方式修改行1中的所有數據爲:\n{}".format(pd_array))

# 開始使用當前的額排序操作
print("開始排序===================")
# 通過當前的index的方式排序 axis = 0表示rows,axis = 1表示columns
print("輸出當前的按照降序排列的數據,按照rows方式:\n{}".format(pd_array.sort_index(axis=0, ascending=False)))
print("輸出當前的按照降序排列的數據,按照columns方式:\n{}".format(pd_array.sort_index(axis=1, ascending=False)))
# 當前的數據是按照行或者列的名稱的方式降序排列

# 通過當前的values的方式排序
print("按照當前的裏面的值的方式開始排序")
print(pd_array.sort_values(by=["列0"]))
# 通過指定當前的by的方式按照指定的方式排序,這個排序是按照當前的data中的數據的大小來排列的,默認當前的ascending=True
print(pd_array.sort_values(by=["列0"], ascending=False))
# 發現當前的行的index的名稱發生了變化
# print(pd_array.sort_values(by=["行0"], ascending=False)) 注意使用sort_values的時候不能按照當前的行index中的值進行排序,報錯了

# 按照多個by方式排序
print("通過多個by方式進行排序:")
print(pd_array.sort_values(by=["列0", "列2"]))

結果:

當前生成的數據爲:
列0 列1 列2 列3 列4
行0 1 6 3 1 5
行1 5 5 5 2 1
行2 1 3 3 6 5
行3 4 8 6 2 7
行4 8 6 2 2 3
輸出修改後的數據:
列0 列1 列2 列3 列4
行0 1 1 3 1 5
行1 5 1 5 2 1
行2 1 1 3 6 5
行3 4 1 6 2 7
行4 8 1 2 2 3
輸出修改後的數據:
列0 列1 列2 列3 列4
行0 1 1 3 1 5
行1 5 1 5 2 1
行2 1 1 3 3 5
行3 4 1 6 4 7
行4 8 1 2 5 3
輸出修改後的數據:
列0 列1 列2 列3 列4
行0 0 0 0 0 0
行1 5 1 5 2 1
行2 1 1 3 3 5
行3 4 1 6 4 7
行4 8 1 2 5 3
使用loc的方式修改行1中的所有數據爲:
列0 列1 列2 列3 列4
行0 0 0 0 0 0
行1 1 2 3 4 5
行2 1 1 3 3 5
行3 4 1 6 4 7
行4 8 1 2 5 3
開始排序===================
輸出當前的按照降序排列的數據,按照rows方式:
列0 列1 列2 列3 列4
行4 8 1 2 5 3
行3 4 1 6 4 7
行2 1 1 3 3 5
行1 1 2 3 4 5
行0 0 0 0 0 0
輸出當前的按照降序排列的數據,按照columns方式:
列4 列3 列2 列1 列0
行0 0 0 0 0 0
行1 5 4 3 2 1
行2 5 3 3 1 1
行3 7 4 6 1 4
行4 3 5 2 1 8
按照當前的裏面的值的方式開始排序
列0 列1 列2 列3 列4
行0 0 0 0 0 0
行1 1 2 3 4 5
行2 1 1 3 3 5
行3 4 1 6 4 7
行4 8 1 2 5 3
列0 列1 列2 列3 列4
行4 8 1 2 5 3
行3 4 1 6 4 7
行1 1 2 3 4 5
行2 1 1 3 3 5
行0 0 0 0 0 0
通過多個by方式進行排序:
列0 列1 列2 列3 列4
行0 0 0 0 0 0
行1 1 2 3 4 5
行2 1 1 3 3 5
行3 4 1 6 4 7
行4 8 1 2 5 3

總結:

1.我們通過切片方式設置數據,但是這個數據只能修改當前的columns中名稱相同的數據,不能修改index名稱的數據,可以使用切片索引方式修改數據【:1】,但是在混合使用的時候必須這樣:pd[列名稱][列索引]

2.可以使用loc方式獲取數據,只能使用loc[行名稱]方式獲取數據不能使用列名稱獲取數據,混合使用的時候必須這樣:pd[行名稱][列名稱]

3.在排序的時候可以按照索引方式排序還可以使用值方式排序

  1. 使用索引方式爲:pd.set_index(),需要指定axis和ascending,其中axis = 0表示rows,axis = 1表示columns,ascending爲True表示按照當升序排列,False表示按照降序排列
  2. 使用值方式爲:pd.set_values(),需要指定by和ascending,這裏的ascending的使用方式和上面的索引方式一致,by爲指定的列索引的名稱,這個值可以是多個
  3. 注意使用值方式排序的時候by不能是行索引的名稱,寫了會報錯的

4.總結

1.使用DataFrame的操作的時候需要主意是使用索引還是名稱方式

2.使用不同的方式實現操作的時候需要注意順序問題

3.在排序的時候set_values只能排序當前的列名稱,使用set_index的時候兩個都可以使用

以上純屬個人見解,如有問題請聯繫本人!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章