[英]Pandas dataframe indexing: Use of '.values' for different index occurrence frequencies
想象一個像這樣的數據框:
A B
ID
1 4 5
2 6 7
2 6 8
如果要訪問A列中ID = 1的值,可以執行以下操作:
df.ix[1, 'A']
對於A列中的ID = 2,此方法有效:
df.ix[2, 'A'].values
有沒有一種方法可以合並兩個語句,而無需使用try / except語句? 我找不到執行此操作的好方法...。
一種方法是使用列表(或元組,切片等)作為索引。 范例-
In [63]: df
Out[63]:
A B
ID
1 4 5
2 6 7
2 8 9
In [64]: df.loc[[1],'A'].values #Works for `.ix` as well.
Out[64]: array([4], dtype=int64)
In [65]: df.loc[[2],'A'].values #Works for `.ix` as well.
Out[65]: array([6, 8], dtype=int64)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.