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 文章收集 ] SQLAlchemy quick start with PostgreSQL

Source From   Here   Preface   This is a quick tutorial for getting started with   SQLAlchemy Core API .   Prerequisites   In this quick st...