Python Forum
Problem with creating DataFrame using Dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with creating DataFrame using Dictionary
#1
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..
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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 ?
Reply
#4
When you were trying what? Show use the code you tried, and show us the syntax error.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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.
Reply
#6
(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().
Reply
#7
(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 ?
Reply
#8
(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.
Reply
#9
I highly suggest you reading and learning pandas basics
https://pandas.pydata.org/pandas-docs/ve...exing.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a Dataframe from Zenodo zip file with multiple CSVs about Spotify man0s 0 1,326 Apr-26-2022, 01:45 PM
Last Post: man0s
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,913 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Create dataframe through Dictionary in pandas ift38375 2 2,188 Aug-11-2019, 01:09 AM
Last Post: boring_accountant
  Creating A List of DataFrames & Manipulating Columns in Each DataFrame firebird 1 4,254 Jul-31-2019, 04:04 AM
Last Post: scidam
  Problem while showing a Pandas DataFrame Sirduke1 0 2,073 Aug-29-2018, 06:14 AM
Last Post: Sirduke1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020