0

Hi I am trying to send a Output as Nested JSON with Table Names as key and All Remaining Columns as string . I have a particular scenario where More than one Table is needed to be sent as Output in JSON I have a pandas Data Frame's like

A =

Descriptive Count   Hunter  Trials
    Hi      21      qwe     12:11
    I       12      wq      13:11
    am      0       qa      14:11
    trying  11      q       15:11
    for     12      a       16:11
    Merged  12      d       17:11
    columns 32      a       18:11

and B =

Descriptive Count   Shooter Trials
    Hi      21      qwe     12:11
    I       12      wq      13:11
    am      0       qa      14:11
    trying  11      q       15:11
    for     12      a       16:11
    Merged  12      d       17:11
    columns 32      a       18:11

Required Output is Data Types of the Tables

When I merged both the tables and took df.dtypes() I got this Output C =

    Column          DataType
    Descriptive     Object
    Count           Integer
    Hunter          Object
    Trials          DateTime
    Descriptive     Object
    Count           Integer
    Shooter         Object
    Trials          DateTime

Required Output for Nested JSON with table Name included is : C =

 Table_Name     Column      DataType
        A       Descriptive Object
                    Count   Integer
                    Hunter  Object
                    Trials  DateTime
        B       Descriptive Object
                    Count   Integer
                    Shooter Object
                    Trials  DateTime

Can you please help me with This

Thanks in Advance .

1 Answers1

0

Its a bit verbose, but

In [21]: pd.concat([a.dtypes.rename('A').to_frame(), b.dtypes.rename('B').to_frame()], axis=1).stack().swaplevel().sort_index()                                                                            
Out[21]: 
A  Count           int64
   Descriptive    object
   Hunter         object
   Trials         object
B  Count           int64
   Descriptive    object
   Shooter        object
   Trials         object
dtype: object

gives your desired result as a DataFrame.

You can concatenate the results of a.dtypes and b.dtypes as shown below. The rename creates columns 'A' and 'B' and stack makes those to indices. Then it is a bit of shuffling around to get the order correct.

maow
  • 2,712
  • 1
  • 11
  • 25