0

I get tables as the attached images. It says class 'pandas.core.frame.DataFrame' when I checked its type. I get the tables by running for loop. I would like to keep merging/joining the tables by using index as below. Thank you for your help in advance! I attached my codes just in case.

Joining Tables

# Loop credentials
for credential in config.api_credentials:
    # Parameters
    security_key = ''
    system_id = credential[0]
    name = credential[1]
    api_id = credential[2]
    api_pass = credential[3]
    solar_id = credential[4]
    solar_tables = {}
    auth_url = "https://example.com/customer/authenticate?login=" + \
        api_id + "&password=" + api_pass + "&json=1"

    res = requests.post(auth_url)
    data = res.json()

    if data.get('securitykey') == None:
        print("There is no securitykey")
    else:
        security_key = data.get('securitykey')

    get_historical_data_url = "https://example.com/location/getHistoricalData?security_key=" + \
        security_key + "&json=1&startTime=2013-02-14T23:59&period=months"
    res2 = requests.get(get_historical_data_url)
    res2_text = res2.text
    production_data = json.loads(res2_text)
    production_data2 = production_data.get(system_id).get('data')

    # Transpose columns and rows
    production_data3 = pd.DataFrame(production_data2).transpose()

    # Drop useless columns and only keep solar production data
    solar_data = production_data3.loc[:, [solar_id]]

    # Extract and replace production values to absolute values
    solar_data[solar_id] = solar_data[solar_id].str.get(
        'kWh').astype(float).abs()

    # Change Label to system id + name
    solar_data.rename(columns={solar_id: system_id+"_"+name}, inplace=True)

    # Change index from datetime to date only
    solar_data.index = solar_data.index.str.split().str[0]

    print(type(solar_data))
    print(solar_data)
yeonhodev
  • 49
  • 7
  • looks like a concat on axis=1 : `pd.concat((df1,df2),axis=1)` – anky Jun 02 '20 at 14:45
  • 1
    df1.join(df2,how='outer') – BENY Jun 02 '20 at 14:46
  • You probably don't want to concat your tables in a for loop: [Why does concatenation of DataFrames get exponentially slower?](https://stackoverflow.com/questions/36489576/why-does-concatenation-of-dataframes-get-exponentially-slower) – bug_spray Jun 02 '20 at 14:48

1 Answers1

1
solar_data_list = []
# Loop credentials
for credential in config.api_credentials:
    # ...
    # at the end of the loop
    # ensure that the solar_data.index is really a DateTimeIndex and not a string
    solar_data_list += [solar_data]

result = pd.concat(solar_data_list, axis=1)

Consider to have a look into how to deal with time series and how to merge, join, and concatenate in pandas.

Michael Dorner
  • 17,587
  • 13
  • 87
  • 117