Problem with creating DataFrame using Dictionary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Problem with creating DataFrame using Dictionary (/thread-19481.html) |
Problem with creating DataFrame using Dictionary - ift38375 - Jul-01-2019 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 : 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 : Plz solve my problem..
RE: Problem with creating DataFrame using Dictionary - ichabod801 - Jul-01-2019 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.
RE: Problem with creating DataFrame using Dictionary - ift38375 - Jul-02-2019 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 ? RE: Problem with creating DataFrame using Dictionary - ichabod801 - Jul-02-2019 When you were trying what? Show use the code you tried, and show us the syntax error. RE: Problem with creating DataFrame using Dictionary - ift38375 - Jul-02-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. RE: Problem with creating DataFrame using Dictionary - scidam - Jul-02-2019 (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() .
RE: Problem with creating DataFrame using Dictionary - ift38375 - Jul-02-2019 (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. 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 ? RE: Problem with creating DataFrame using Dictionary - scidam - Jul-02-2019 (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. RE: Problem with creating DataFrame using Dictionary - ThomasL - Jul-02-2019 I highly suggest you reading and learning pandas basics https://pandas.pydata.org/pandas-docs/version/0.15/indexing.html |