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
#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


Messages In This Thread
RE: Problem with creating DataFrame using Dictionary - by ift38375 - Jul-02-2019, 07:11 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a Dataframe from Zenodo zip file with multiple CSVs about Spotify man0s 0 1,382 Apr-26-2022, 01:45 PM
Last Post: man0s
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 3,014 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Create dataframe through Dictionary in pandas ift38375 2 2,276 Aug-11-2019, 01:09 AM
Last Post: boring_accountant
  Creating A List of DataFrames & Manipulating Columns in Each DataFrame firebird 1 4,353 Jul-31-2019, 04:04 AM
Last Post: scidam
  Problem while showing a Pandas DataFrame Sirduke1 0 2,115 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