2018年3月5日 星期一

[ Python 常見問題 ] Pandas - Get column index from column name in python pandas

Source From Here 
Question 
Need to retrieve a column index based on the name of the column from Pandas. 

How-To 
Check below sample code for pandas.Index.get_loc: 
>>> import pandas as pd 
>>> df = pd.DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]}) 
>>> df.columns 
Index([u'apple', u'orange', u'pear'], dtype='object') 
>>> df.columns.get_loc("pear") 
2


沒有留言:

張貼留言

[ Python 文章收集 ] List Comprehensions and Generator Expressions

Source From  Here   Preface   Do you know the difference between the following syntax?  view plain copy to clipboard print ? [x  for ...