![](/img/trans.png)
[英]Python Pandas DataFrame column calls by column index but not name?
[英]Can I mix Python dataframe positional index and column name?
我可以使用負數\/位置索引和列名在數據框中獲取條目嗎? 例如,
import pandas as pd
df = pd.DataFrame({'a':[0,1,2], 'b':[3,4,5]})
# df is:
# a b
# 0 0 3
# 1 1 4
# 2 2 5
df.iloc[-1]['a'] # got 2
df['a'].iloc[-1] # got 2
df.loc[1,'a'] # got 1
df.loc[-1,'a'] # got KeyError: -1
不,你不能在同一個
iloc<\/code> \/
loc<\/code>中混合兩者(這曾經是現在不推薦使用的
ix<\/code><\/a>索引器的情況)
正確的方法是雙重切片之一:
df.iloc[-1]['a']
不,使用
df.columns<\/code>和
get_loc<\/code>將列標題標簽轉換為索引位置:
df.iloc[-1, df.columns.get_loc('a')]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.