Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Create dataframe through Dictionary in pandas
#1
Hi,

I have given below table and i want to create dataframe of this table using Dictionary:

          2019        2017        2015       2013      2011
qtr1      61000       44900       5000       5000      5000
qtr3      NaN         57000       5000       5000      5000
qtr4      NaN         59000       5000       5000      5000
qtr       NaN         NaN         NaN        NaN       NaN

I am using given below code but no luck :
import pandas as pd
df2 = { 2019 :{ 'qtr1':61000, } ,
        2017 :{ 'qtr1':44900,'qtr3':57000,'qtr4':59000},
        2015 :{ 'qtr1':5000,'qtr3':5000,'qtr4':5000},
        2013 :{ 'qtr1':5000,'qtr3':5000,'qtr4':5000},
        2011 :{ 'qtr1':5000,'qtr3':5000,'qtr4':5000}
        }
ndf = pd.DataFrame(df2)
print(ndf)


How can i declare whole row of "qtr' as NaN here i am confused ?
Quote
#2
Append qtr somewhere to the dictionary, e.g.
2011 :{ 'qtr1':5000,'qtr3':5000,'qtr4':5000, 'qtr': None}.
Quote
#3
The following should probably do what you want.
import pandas as pd
df2 = { 2019 :{ 'qtr1':61000, } ,
        2017 :{ 'qtr1':44900,'qtr3':57000,'qtr4':59000},
        2015 :{ 'qtr1':5000,'qtr3':5000,'qtr4':5000},
        2013 :{ 'qtr1':5000,'qtr3':5000,'qtr4':5000},
        2011 :{ 'qtr1':5000,'qtr3':5000,'qtr4':5000}
        }
ndf = pd.DataFrame(df2)
ndf = ndf.append(pd.DataFrame(index=['qtr']))
print(ndf)
Output:
2011 2013 2015 2017 2019 qtr1 5000.0 5000.0 5000.0 44900.0 61000.0 qtr3 5000.0 5000.0 5000.0 57000.0 NaN qtr4 5000.0 5000.0 5000.0 59000.0 NaN qtr NaN NaN NaN NaN NaN
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Obtaining Correct Date In Pandas DataFrame eddywinch82 14 1,083 Feb-17-2020, 11:45 AM
Last Post: eddywinch82
  Ordering of pandas DataFrame new_to_python 2 125 Feb-08-2020, 01:55 PM
Last Post: new_to_python
  Parse XML String in Pandas Dataframe creedX 2 467 Dec-09-2019, 07:35 PM
Last Post: creedX
  Pandas dataframe to join three tables using like condition among them sandeep_ganga 0 289 Nov-29-2019, 08:30 AM
Last Post: sandeep_ganga
  Pandas Dataframe to Google Big Query Ecniv 2 973 Nov-21-2019, 02:26 PM
Last Post: Ecniv
  manipulating a dataframe - pandas nsx200 2 277 Nov-14-2019, 10:38 AM
Last Post: nsx200
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 316 Nov-04-2019, 07:00 AM
Last Post: UniKlixX
  pandas dataframe iloc mystery edvvardbrian 2 363 Oct-29-2019, 02:55 PM
Last Post: jefsummers
  How to add a few empty rows into a pandas dataframe python_newbie09 2 1,884 Sep-20-2019, 08:52 AM
Last Post: python_newbie09
  Dropping a column from pandas dataframe marco_ita 6 2,145 Sep-07-2019, 08:36 AM
Last Post: marco_ita

Forum Jump:


Users browsing this thread: 1 Guest(s)