Posts: 29
Threads: 18
Joined: Apr 2019
Jul-01-2019, 04:30 PM
(This post was last modified: Jul-01-2019, 04:30 PM by ift38375.)
Hi,
When i am creating dataframe for desired table then it is working fine as given below
import pandas as pd
diSales= { 2016:{'qtr1':34500,'qtr2':56000,'qtr3':47000,'qtr4':49000},
2017:{'qtr1':44900,'qtr2':46100,'qtr3':57000,'qtr4':59000},
2018:{'qtr1':54500,'qtr2':51000,'qtr3':57000,'qtr4':58500},
2019:{'qtr1':61000}
}
ks = pd.DataFrame(diSales)
print(ks) and output for given code is :
Output: 2016 2017 2018 2019
qtr1 34500 44900 54500 61000.0
qtr2 56000 46100 51000 NaN
qtr3 47000 57000 57000 NaN
qtr4 49000 59000 58500 NaN
But when i am trying to change Column to Mon, Tues , Wed, Thrus then it is giving error
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}
}
ks = pd.DataFrame(diSales)
print(ks) then it is showing error in output as :
Output: Traceback (most recent call last):
File "C:\Users\Bhavik\Desktop\agg.py", line 2, in <module>
diSales= { Mon:{'qtr1':34500,'qtr2':56000,'qtr3':47000,'qtr4':49000},
NameError: name 'Mon' is not defined
Plz solve my problem..
Posts: 4,229
Threads: 97
Joined: Sep 2016
Mon is a name in Python, not a string. It needs something it is associated with, like an integer or a function or a class. The error is saying you haven't made that association yet. 'Mon' (with quotes) is a string value, suitable to be used as a key in a dictionary.
Posts: 29
Threads: 18
Joined: Apr 2019
Jul-02-2019, 01:04 AM
(This post was last modified: Jul-02-2019, 01:04 AM by ift38375.)
I want to count particular column occurrence in Dataframe with given below Dataframe ?
import pandas as pd
diSales= { 2016:{'qtr1':34500,'qtr2':56000,'qtr3':47000,'qtr4':49000},
2017:{'qtr1':44900,'qtr2':46100,'qtr3':57000,'qtr4':59000},
2018:{'qtr1':54500,'qtr2':51000,'qtr3':57000,'qtr4':58500},
2019:{'qtr1':61000}
}
ks = pd.DataFrame(diSales)
ptr = ks.2016.count()
print(ptr) but when i was trying then showing error " invalid syntax ?
is dataframe allow operation only on alphabetical column names instead of numeric column name ?
Posts: 4,229
Threads: 97
Joined: Sep 2016
When you were trying what? Show use the code you tried, and show us the syntax error.
Posts: 29
Threads: 18
Joined: Apr 2019
(Jul-02-2019, 02:22 AM)ichabod801 Wrote: When you were trying what? Show use the code you tried, and show us the syntax error.
Sir,
I already shared code and error comes with line "ks.2016.count()" bcoz it not takes value 2016 as column.
Posts: 818
Threads: 1
Joined: Mar 2018
(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() .
Posts: 29
Threads: 18
Joined: Apr 2019
Jul-02-2019, 07:11 AM
(This post was last modified: Jul-02-2019, 07:11 AM by ift38375.)
(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 ?
Posts: 818
Threads: 1
Joined: Mar 2018
(Jul-02-2019, 07:11 AM)ift38375 Wrote: Why ? I don't really know how to answer to the question. It has at least short and long answers. The former is simple -- names of Python objects (names of functions, classes, attributes, instance methods, etc) should not start with a digit (you can think here that you are trying to get access to the instance attribute/or method starting with a digit), the latter will require you to dive into Python grammar and programming language implementation.
Posts: 361
Threads: 5
Joined: Jun 2019
I highly suggest you reading and learning pandas basics
https://pandas.pydata.org/pandas-docs/ve...exing.html
|