python - Pandas Passing Variable Names into Column Name -


i have datafram contains 13 different column names, have separated these headings 2 lists. want preform different operations on each of these lists.

is possible pass column names pandas variable? code @ moment can loop through list fine having trouble trying pass column name function

code

cont = ['age','fnlwgt','capital-gain','capital-loss'] #loops through columns column_name, column in df.transpose().iterrows():     if column_name in cont:         x = column_name         print(df.x.count())     else:         print('') 

i think can use subset created list cont:

print df   age fnlwgt  capital-gain 0      9th             5 1   b    9th             6 2   c    8th             3  cont = ['age','fnlwgt']  print df[cont]   age fnlwgt 0      9th 1   b    9th 2   c    8th  print df[cont].count() age       3 fnlwgt    3 dtype: int64  print df[['capital-gain']]    capital-gain 0             5 1             6 2             3 

maybe better list dictionary, created to_dict:

d = df[cont].count().to_dict() print d {'age': 3, 'fnlwgt': 3} print d['age'] 3 print d['fnlwgt'] 3 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -