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


沒有留言:

張貼留言

[Linux 常見問題] How to set a file size limit for a directory?

Source From   Here   Question   I   have a directory on my system which is used for a specific reason by applications and users , but I   d...