簡體   English   中英

檢查 pandas dataframe 並在同一行的其他列中顯示其他元素的最快方法

[英]Fastest way to check pandas dataframe and show other elements in the other columns at the same row

如果有要檢查的單詞列表...

word_list = ['word1', 'word2', 'word3']

和一個像

Word,Score_a,Score_b,Score_c
word5,10,15,20
word6,40,60,80
word3,40,20,10

找到給定單詞列表中每個單詞的對應分數的最快方法是什么?

例如40, 20, 10 'word3'

詳細說明上面的評論:

df.set_index('Word') #set Word column as index
df.loc['word5', :] # access row

Output:

 Score_a    10
 Score_b    15
 Score_c    20

如果不想將 Word 設置為索引,也可以使用.iloc

df.iloc[0, :]

Output:

Word        word5
 Score_a       10
 Score_b       15
 Score_c       20

請注意,兩者之間的主要區別(除了您可以使用loc指定列名這一事實之外)是loc在您指定范圍時包含最后一項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM