python pandas.DataFrame.loc函數(shù)使用詳解
官方函數(shù)
DataFrame.loc
Access a group of rows and columns by label(s) or a boolean array.
.loc[] is primarily label based, but may also be used with a boolean array.
# 可以使用label值,但是也可以使用布爾值
- Allowed inputs are: # 可以接受單個(gè)的label,多個(gè)label的列表,多個(gè)label的切片
- A single label, e.g. 5 or ‘a(chǎn)', (note that 5 is interpreted as a label of the index, and never as an integer position along the index). #這里的5不是數(shù)值指定的位置,而是label值
- A list or array of labels, e.g. [‘a(chǎn)', ‘b', ‘c'].
slice object with labels, e.g. ‘a(chǎn)':'f'.
Warning: #如果使用多個(gè)label的切片,那么切片的起始位置都是包含的
Note that contrary to usual python slices, both the start and the stop are included
- A boolean array of the same length as the axis being sliced, e.g. [True, False, True].
實(shí)例詳解
一、選擇數(shù)值
1、生成df
df = pd.DataFrame([[1, 2], [4, 5], [7, 8]], ... index=['cobra', 'viper', 'sidewinder'], ... columns=['max_speed', 'shield']) df Out[15]: max_speed shield cobra 1 2 viper 4 5 sidewinder 7 8
2、Single label. 單個(gè) row_label 返回的Series
df.loc['viper'] Out[17]: max_speed 4 shield 5 Name: viper, dtype: int64
2、List of labels. 列表 row_label 返回的DataFrame
df.loc[['cobra','viper']] Out[20]: max_speed shield cobra 1 2 viper 4 5
3、Single label for row and column 同時(shí)選定行和列
df.loc['cobra', 'shield'] Out[24]: 2
4、Slice with labels for row and single label for column. As mentioned above, note that both the start and stop of the slice are included. 同時(shí)選定多個(gè)行和單個(gè)列,注意的是通過列表選定多個(gè)row label 時(shí),首位均是選定的。
df.loc['cobra':'viper', 'max_speed'] Out[25]: cobra 1 viper 4 Name: max_speed, dtype: int64
5、Boolean list with the same length as the row axis 布爾列表選擇row label
布爾值列表是根據(jù)某個(gè)位置的True or False 來選定,如果某個(gè)位置的布爾值是True,則選定該row
df Out[30]: max_speed shield cobra 1 2 viper 4 5 sidewinder 7 8 df.loc[[True]] Out[31]: max_speed shield cobra 1 2 df.loc[[True,False]] Out[32]: max_speed shield cobra 1 2 df.loc[[True,False,True]] Out[33]: max_speed shield cobra 1 2 sidewinder 7 8
6、Conditional that returns a boolean Series 條件布爾值
df.loc[df['shield'] > 6] Out[34]: max_speed shield sidewinder 7 8
7、Conditional that returns a boolean Series with column labels specified 條件布爾值和具體某列的數(shù)據(jù)
df.loc[df['shield'] > 6, ['max_speed']] Out[35]: max_speed sidewinder 7
8、Callable that returns a boolean Series 通過函數(shù)得到布爾結(jié)果選定數(shù)據(jù)
df Out[37]: max_speed shield cobra 1 2 viper 4 5 sidewinder 7 8 df.loc[lambda df: df['shield'] == 8] Out[38]: max_speed shield sidewinder 7 8
二、賦值
1、Set value for all items matching the list of labels 根據(jù)某列表選定的row 及某列 column 賦值
df.loc[['viper', 'sidewinder'], ['shield']] = 50 df Out[43]: max_speed shield cobra 1 2 viper 4 50 sidewinder 7 50
2、Set value for an entire row 將某行row的數(shù)據(jù)全部賦值
df.loc['cobra'] =10 df Out[48]: max_speed shield cobra 10 10 viper 4 50 sidewinder 7 50
3、Set value for an entire column 將某列的數(shù)據(jù)完全賦值
df.loc[:, 'max_speed'] = 30 df Out[50]: max_speed shield cobra 30 10 viper 30 50 sidewinder 30 50
4、Set value for rows matching callable condition 條件選定rows賦值
df.loc[df['shield'] > 35] = 0 df Out[52]: max_speed shield cobra 30 10 viper 0 0 sidewinder 0 0
三、行索引是數(shù)值
df = pd.DataFrame([[1, 2], [4, 5], [7, 8]], ... index=[7, 8, 9], columns=['max_speed', 'shield']) df Out[54]: max_speed shield 7 1 2 8 4 5 9 7 8
通過 行 rows的切片的方式取多個(gè):
df.loc[7:9] Out[55]: max_speed shield 7 1 2 8 4 5 9 7 8
四、多維索引
1、生成多維索引
tuples = [ ... ('cobra', 'mark i'), ('cobra', 'mark ii'), ... ('sidewinder', 'mark i'), ('sidewinder', 'mark ii'), ... ('viper', 'mark ii'), ('viper', 'mark iii') ... ] index = pd.MultiIndex.from_tuples(tuples) values = [[12, 2], [0, 4], [10, 20], ... [1, 4], [7, 1], [16, 36]] df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index) df Out[57]: max_speed shield cobra mark i 12 2 mark ii 0 4 sidewinder mark i 10 20 mark ii 1 4 viper mark ii 7 1 mark iii 16 36
2、Single label. 傳入的就是最外層的row label,返回DataFrame
df.loc['cobra'] Out[58]: max_speed shield mark i 12 2 mark ii 0 4
3、Single index tuple.傳入的是索引元組,返回Series
df.loc[('cobra', 'mark ii')] Out[59]: max_speed 0 shield 4 Name: (cobra, mark ii), dtype: int64
4、Single label for row and column.如果傳入的是row和column,和傳入tuple是類似的,返回Series
df.loc['cobra', 'mark i'] Out[60]: max_speed 12 shield 2 Name: (cobra, mark i), dtype: int64
5、Single tuple. Note using [[ ]] returns a DataFrame.傳入一個(gè)數(shù)組,返回一個(gè)DataFrame
df.loc[[('cobra', 'mark ii')]] Out[61]: max_speed shield cobra mark ii 0 4
6、Single tuple for the index with a single label for the column 獲取某個(gè)colum的某row的數(shù)據(jù),需要左邊傳入多維索引的tuple,然后再傳入column
df.loc[('cobra', 'mark i'), 'shield'] Out[62]: 2
7、傳入多維索引和單個(gè)索引的切片:
df.loc[('cobra', 'mark i'):'viper'] Out[63]: max_speed shield cobra mark i 12 2 mark ii 0 4 sidewinder mark i 10 20 mark ii 1 4 viper mark ii 7 1 mark iii 16 36 df.loc[('cobra', 'mark i'):'sidewinder'] Out[64]: max_speed shield cobra mark i 12 2 mark ii 0 4 sidewinder mark i 10 20 mark ii 1 4 df.loc[('cobra', 'mark i'):('sidewinder','mark i')] Out[65]: max_speed shield cobra mark i 12 2 mark ii 0 4 sidewinder mark i 10 20
到此這篇關(guān)于python pandas.DataFrame.loc函數(shù)使用詳解的文章就介紹到這了,更多相關(guān)pandas.DataFrame.loc函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python?pandas?DataFrame基礎(chǔ)運(yùn)算及空值填充詳解
- Python數(shù)據(jù)分析之?Pandas?Dataframe條件篩選遍歷詳情
- Python數(shù)據(jù)分析之?Pandas?Dataframe合并和去重操作
- Python數(shù)據(jù)分析Pandas?Dataframe排序操作
- python?pandas分割DataFrame中的字符串及元組的方法實(shí)現(xiàn)
- Python pandas.DataFrame 找出有空值的行
- Python pandas.DataFrame調(diào)整列順序及修改index名的方法
- python?Pandas之DataFrame索引及選取數(shù)據(jù)
相關(guān)文章
Python網(wǎng)絡(luò)編程之ZeroMQ知識(shí)總結(jié)
這篇文章主要介紹了Python網(wǎng)絡(luò)編程之ZeroMQ知識(shí)總結(jié),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04Python使用Tkinter庫如何設(shè)置tkinter ttk框架背景顏色
本文介紹了在Python的Tkinter庫中,如何使用style.configure方法為ttk框架設(shè)置背景顏色及其他樣式屬性,以定制美觀的GUI界面2024-09-09對(duì)python產(chǎn)生隨機(jī)的二維數(shù)組實(shí)例詳解
今天小編就為大家分享一篇對(duì)python產(chǎn)生隨機(jī)的二維數(shù)組實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12使用Python實(shí)現(xiàn)遺傳算法的完整代碼
這篇文章主要介紹了使用Python實(shí)現(xiàn)遺傳算法,其本質(zhì)是一種高效、并行、全局搜索的方法,自適應(yīng)的控制搜索過程以求得最優(yōu)解,需要的朋友可以參考下2023-03-03Python隨機(jī)函數(shù)庫random的使用方法詳解
這篇文章主要介紹了Python隨機(jī)函數(shù)庫random的使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08python?tkinter實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python?tkinter實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02用Python編寫腳本使IE實(shí)現(xiàn)代理上網(wǎng)的教程
這篇文章主要介紹了用Python編寫腳本使IE實(shí)現(xiàn)代理上網(wǎng)的教程,“著名的”goagent代理也是基于同樣原理實(shí)現(xiàn),需要的朋友可以參考下2015-04-04利用Pycharm斷點(diǎn)調(diào)試Python程序的方法
今天小編就為大家分享一篇利用Pycharm斷點(diǎn)調(diào)試Python程序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11