(Jul-02-2019, 03:22 AM)scidam Wrote:(Jul-02-2019, 02:46 AM)ift38375 Wrote: I already shared code and error comes with line "ks.2016.count()" bcoz it not takes value 2016 as column.Pandas allows to get access to columns of a dataframe instance as its attributes, e.g.df.column_name
, but you need to be careful when using this approach. It is better to use.loc
selector instead, e.g.df.loc[:, 2016].count()
.
when i am doing same thing with given dataframe then no error comes with alphabetical column name:
import pandas as pd diSales= { 'Mon':{'qtr1':34500,'qtr2':56000,'qtr3':47000,'qtr4':49000}, 'Tues':{'qtr1':44900,'qtr2':46100,'qtr3':57000,'qtr4':59000}, 'Wed':{'qtr1':54500,'qtr2':51000,'qtr3':57000,'qtr4':58500}, 'Thrus':{'qtr1':61000} } df = pd.DataFrame(diSales) ks = df.Mon.count() print(ks)It gives output '4'
But when i am changing column names and taking numeric form column name as 2016,2017,2018,2019 so on. then it gives error of " Invalid syntax" when i am doing
ks = df.2016.count() .
Why ?