OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

ValueError: No axis named "type" for object type <class 'pandas.core.frame.DataFrame

  • Thread starter Thread starter Dinosaurius
  • Start date Start date
D

Dinosaurius

Guest
This is my data frame df received after some preprocessing:

Code:
    name    type    range   time                number
0   XXXX    11111   1-120   [1-120, 261.05]     [1-120, 23229]
1   XXXX    11111   121-240 [121-240, 277.79]   [121-240, 6382]
2   XXXX    11111   241-360 [241-360, 269.64]   [241-360, 2769]
3   XXXX    11111   361-480 [361-480, 279.09]   [361-480, 1331]

I want to transform it into the following json string:

Code:
[{"name":"XXXX","type":11111,"time":[["1-120",261.05],["121-240",277.79],["241-360",269.64],["361-480",279.09]],"number":[["1-120",23229],["121-240",6382],["241-360",2769],["361-480",1331]]

If I do it this way, I get an error ValueError: No axis named "type" for object type <class 'pandas.core.frame.DataFrame'>:

Code:
jsonresult = df.groupby('name','type')['time','number'].agg(lambda x : x.tolist()).reset_index().to_json(orient='records')
jsonresult

It only works if I do df.groupby('name') or df.groupby('type').
<p>This is my data frame <code>df</code> received after some preprocessing:</p>

<pre><code> name type range time number
0 XXXX 11111 1-120 [1-120, 261.05] [1-120, 23229]
1 XXXX 11111 121-240 [121-240, 277.79] [121-240, 6382]
2 XXXX 11111 241-360 [241-360, 269.64] [241-360, 2769]
3 XXXX 11111 361-480 [361-480, 279.09] [361-480, 1331]
</code></pre>

<p>I want to transform it into the following json string:</p>

<pre><code>[{"name":"XXXX","type":11111,"time":[["1-120",261.05],["121-240",277.79],["241-360",269.64],["361-480",279.09]],"number":[["1-120",23229],["121-240",6382],["241-360",2769],["361-480",1331]]
</code></pre>

<p>If I do it this way, I get an error <code>ValueError: No axis named "type" for object type <class 'pandas.core.frame.DataFrame'></code>:</p>

<pre><code>jsonresult = df.groupby('name','type')['time','number'].agg(lambda x : x.tolist()).reset_index().to_json(orient='records')
jsonresult
</code></pre>

<p>It only works if I do <code>df.groupby('name')</code> or <code>df.groupby('type')</code>.</p>
 

Latest posts

Top