given following data frame:
import pandas pd import numpy np df = pd.dataframe({'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9]}) df b c 0 1 4 7 1 2 5 8 2 3 6 9
how can access columns via variable?
i tried this:
cols='a','b' df[cols]
...which resulted in this:
keyerror: ('a', 'b')
bonus question: if data frame this?:
import pandas pd import numpy np df = pd.dataframe({'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9], 'd':[1,3,5], 'e':[5,3,6], 'f':[7,4,3]}) df b c d e f 0 1 4 7 1 5 7 1 2 5 8 3 3 4 2 3 6 9 5 6 3
and wanted this?:
cols=['a','b'] cols2=['c','d'] df[cols,'f',cols2]
thanks in advance!
you can try subset list
of column names:
cols=['a','b'] print df[cols] b 0 1 4 1 2 5 2 3 6
it same as:
print df[['a','b']] b 0 1 4 1 2 5 2 3 6
bonus answer:
cols=['a','b'] cols2=['c','d'] allcols = cols + ['f'] + cols2 print df[allcols] b f c d 0 1 4 7 7 1 1 2 5 4 8 3 2 3 6 3 9 5
Comments
Post a Comment