0

I am trying to convert a JSON file into a dataframe. The dataframe I created has two columns: time and sumOfFFT. They both are floating point numbers. While sumOfFFT is of type float, time is of the type object and each row in the column is a list with the number in it.

I tried using to_numeric and all the digits get converted to NaN since it's a list object. I also tried Series but that did the same. I am trying to convert the list object in every row to a string and then float but it throws an error.

a = float(str(frame.iloc[1]['time']))
print(a)
ValueError                                Traceback (most recent call last)
<ipython-input-75-2f41310a23f7> in <module>()
----> 1 a = float(str(frame.iloc[1]['time']))
      2 print(a)

ValueError: could not convert string to float: '[0.0001041666667]'

Data format:

{
    "fft": {
        "time": [
            [0],
            [0.0001041666667],
            [0.0002083333333],
            [0.0003125],
                        ],
        "sumOfFFT": [2.883648107,2.769599456,2.659837554,2.560352381]
                }
}

Code so far:

import json
import pandas as pd
with open('akhil_195682_170628-174745.json') as json_data:
   obj = json.load(json_data)
   frame = pd.DataFrame(obj['fft'], columns=['time', 'sumOfFFT'])

Output:

time    sumOfFFT
0   [0] 2.883648
1   [0.0001041666667]   2.769599
2   [0.0002083333333]   2.659838
3   [0.0003125] 2.560352

I expect to convert the data into a DataFrame, and plot the two variables against each other.

Aman Deep Middha
  • 65
  • 1
  • 1
  • 6

1 Answers1

0

You can do it like this:

frame["time"]=frame["time"].apply(lambda x:x[0])

Result:

    sumOfFFT    time
0   2.883648    0.000000
1   2.769599    0.000104
2   2.659838    0.000208
3   2.560352    0.000313
Klemen Koleša
  • 446
  • 3
  • 6