Jacob
Jacob

Reputation: 11

DataFrame is created for each index in loop

I am writing a script to take time series data. The idea is to only take the last timestamp and corresponding value and create a whole DataFrame, that contains the last 'ts' and 'value' in each entityId. However, we encountered a problem in our loop, which created a DataFrame for each row (as shown below), which is 120 DataFrames. This should ideally be 1 DataFrame.

so this is the code:

for i, ids in enumerate(data.id):
    URL2 = baseURL + "TimeValues" + "?entityId=" + ids
    timevalues = requests.get(URL2).json()
    df_timevalues = pd.DataFrame(timevalues)
    filtered_timevalues = df_timevalues.tail(1)
    print(filtered_timevalues)

And the problem is this:

                  ts         value
35  2020-04-07T08:15:15Z  23940.300781
                  ts         value
35  2020-04-07T08:45:15Z  10154.099609
                  ts         value
35  2020-04-07T08:49:44Z  7157.200195

Upvotes: 1

Views: 31

Answers (1)

Peter
Peter

Reputation: 12295

You need to append the received data to the existing dataframe:

filtered_timevalues = filtered_timevalues.append(
    df_timevalues, ignore_index=True)

Upvotes: 1

Related Questions