在Pandas中我們主要通過以下幾個函數(shù)來定位DataFrame中的特定數(shù)據(jù)
- iloc
- loc
- iat
- at
總的來說,分為兩種:
一種是通過lables(即row index和column names,這里row index可以字符魁袜,日期等非數(shù)字index)(使用loc, at);
另一種通過index(這里特指數(shù)字位置index)(使用iloc, iat)
loc和at的區(qū)別在于从祝, loc可以選擇特定的行或列穴翩,但是at只能定位某個特定的值土榴,標(biāo)量值氨菇。一般情況下儡炼,我們iloc和loc更加通用,而at, iat有一定的性能提升查蓉。
具體示例可以參考Reference中StackOverflow的示例
下面展示一些特別的:
In [630]: df
Out[630]:
age color food height score state
Jane 30 blue Steak 165 4.6 NY
Nick 2 green Lamb 70 8.3 TX
Aaron 12 red Mango 120 9.0 FL
Penelope 4 white Apple 80 3.3 AL
Dean 32 gray Cheese 180 1.8 AK
Christina 33 black Melon 172 9.5 TX
Cornelia 69 red Beans 150 2.2 TX
# 選擇某一行數(shù)據(jù)
In [631]: df.loc['Dean']
Out[631]:
age 32
color gray
food Cheese
height 180
score 1.8
state AK
Name: Dean, dtype: object
# 選擇某一列數(shù)據(jù)乌询,逗號前面是行的label,逗號后邊是列的label豌研,使用":"來表示選取所有的妹田,本例是選取所有的行,當(dāng)':'在逗號后邊時表示選取所有的列聂沙,但通常我們可以省略秆麸。
In [241]: df.loc[:, 'color']
Out[241]:
Jane blue
Nick green
Aaron red
Penelope white
Dean gray
Christina black
Cornelia red
Name: color, dtype: object
# 也可以如下選取一列,但是與前者是有區(qū)別的及汉,具體參考Reference中的《Returning a view versus a copy》
In [632]: df.loc[:]['color']
Out[632]:
Jane blue
Nick green
Aaron red
Penelope white
Dean gray
Christina black
Cornelia red
Name: color, dtype: object
# 選擇某幾行數(shù)據(jù),注意無論選擇多行還是多列屯烦,都需要將其label放在一個數(shù)組當(dāng)中坷随,而選擇單個行或列,則不需要放在數(shù)組當(dāng)中
In [634]: df.loc[['Nick', 'Dean']]
Out[634]:
age color food height score state
Nick 2 green Lamb 70 8.3 TX
Dean 32 gray Cheese 180 1.8 AK
# 注意以下這種用法不行驻龟,這是由于Pandas會認(rèn)為逗號后邊是列的label
df.loc['Nick', 'Dean']
# 選擇范圍
In [636]: df.loc['Nick':'Christina']
Out[636]:
age color food height score state
Nick 2 green Lamb 70 8.3 TX
Aaron 12 red Mango 120 9.0 FL
Penelope 4 white Apple 80 3.3 AL
Dean 32 gray Cheese 180 1.8 AK
Christina 33 black Melon 172 9.5 TX
# iloc的特定用法, 可以用-1這樣index來獲取最后一行的數(shù)據(jù)
In [637]: df.iloc[[1, -1]]
Out[637]:
age color food height score state
Nick 2 green Lamb 70 8.3 TX
Cornelia 69 red Beans 150 2.2 TX
數(shù)據(jù)定位是后續(xù)條件過濾温眉、賦值以及各種轉(zhuǎn)換的基礎(chǔ),一定要熟練掌握翁狐。
另外类溢,在定位某一個具體的元素的時候,loc和at并不完全相同
# loc支持以下兩種定位方式
In [726]: df.loc['Jane', 'score']
Out[726]: 4.6
In [727]: df.loc['Jane']['score']
Out[727]: 4.6
# 但是at只支持第一種定位方式
In [729]: df.at['Nick', 'height']
Out[729]: 181
In [730]: df.at['Nick']['height']
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-730-948408df1727> in <module>()
----> 1 df.at['Nick']['height']
~/.pyenv/versions/3.6.4/envs/data_analysis/lib/python3.6/site-packages/pandas/core/indexing.py in __getitem__(self, key)
1867
1868 key = self._convert_key(key)
-> 1869 return self.obj._get_value(*key, takeable=self._takeable)
1870
1871 def __setitem__(self, key, value):
TypeError: _get_value() missing 1 required positional argument: 'col'
有兩點(diǎn)需要說明:
- 在針對特定元素賦值的時候最好使用at來進(jìn)行操作露懒,性能提升還是很明顯的闯冷。
- loc的兩種方式并不等同,
df.loc['Jane', 'score']
是在同一塊內(nèi)存中對數(shù)據(jù)進(jìn)行操作懈词,而df.loc['Jane']['score']
是在另一個copy上進(jìn)行操作蛇耀,具體參考Returning a view versus a copy