如何使用Pandas将单个行附加到一个数据帧?

2024-05-15 02:02:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个正在打印单个行的数据帧,我需要将所有行追加或连接到一个数据帧中。这里是我的hrlyèdf数据帧的一个例子。每一行都有索引0,这意味着它是单独添加的。我还没有弄清楚下面的代码中需要更改什么。我希望我的输出像这样

欲望输出

    field id |   HourlyPrecipIn | HourlyRH | HourlyTempF | dateTime
0   40238        0.00             98         56.5          2019-05-22 01:00:00 
1   40238        0.01             95         55.5          2019-05-22 02:00:00 
2   40238        0.02             96         57.7          2019-05-22 03:00:00 

电流输出

HourlyPrecipIn HourlyRH HourlyTempF dateTime field id 0 0.0 98.8 44.9 2019-05-22 01:00:00 40238 HourlyPrecipIn HourlyRH HourlyTempF dateTime field id 0 0.0 98.9 45.1 2019-05-22 02:00:00 40238 HourlyPrecipIn HourlyRH HourlyTempF dateTime field id 0 0.0 98.7 45.1 2019-05-22 03:00:00 40238 HourlyPrecipIn HourlyRH HourlyTempF dateTime field id 0 0.02 99.6 45.3 2019-05-22 04:00:00 40238 HourlyPrecipIn HourlyRH HourlyTempF dateTime field id 0 0.0 95.0 46.7 2019-05-22 05:00:00 40238

代码:

`for lat, lon, id_, in zip(latval, lonval, idVal):

                    print 'Starting import of field id: {0}'.format(id_)
                    time_param = '?start='+ config.dayVal +'T'+ '06:00:00' + 'Z' + '&end='+ config.todayVal+ 'T'+ '05:00'+'Z'

                    #########HOURLY VALUES############
                    hrTemp = 'https://insight.api.wdtinc.com/hourly-temperature/' + str(lat)+'/' + str(lon) + time_param + '&unit=fahrenheit'
                    hrHumidity =  'https://insight.api.wdtinc.com/hourly-relative-humidity/' + str(lat)+'/' + str(lon) + time_param
                    hrPrecip = 'https://insight.api.wdtinc.com/hourly-precipitation/' + str(lat)+'/' + str(lon) + time_param + '&unit=inches'


                    resp_pre_hr = se_.get(hrPrecip, auth=(config.dtn_app_id, config.dtn_key), timeout=30)
                    resp_temp_hr = se_.get(hrTemp, auth=(config.dtn_app_id, config.dtn_key), timeout=60)
                    resp_rh_hr = se_.get(hrHumidity, auth=(config.dtn_app_id, config.dtn_key), timeout=90)

                    preValHrly = json.loads(resp_pre_hr.content)
                    tempValHrly = json.loads(resp_temp_hr.content)
                    humidityHrly = json.loads(resp_rh_hr.content)

                    logging.info (hrHumidity)
                    logging.info (humidityHrly)

                    aHumidVal = humidityHrly['series']
                    aPreVal =  preValHrly['series']
                    aTempVal = tempValHrly['series']
                    aTimeVal = tempValHrly['series']


                    for aHumidValJ, aTempValJ, aPreValJ, aTimeJ in zip (aHumidVal, aTempVal , aPreVal, aTimeVal):
                        aHumidVJ = aHumidValJ['value']
                        aTempVJ =  aTempValJ['value']
                        aPreVJ = aPreValJ['value']
                        aTimeVJ = aTimeJ['validTime']
                        aTimeVJ = aTimeVJ[:-1]
                        aTimeVJ = aTimeVJ.replace("T", " ")

                        from_zone = tz.gettz('UTC')
                        to_zone = tz.gettz('America/Chicago') 
                        utc = datetime.strptime(aTimeVJ, DATE_TIME_FORMAT)
                        utc = utc.replace(tzinfo=from_zone)
                        central = utc.astimezone(to_zone)
                        t = central.strftime(DATE_TIME_FORMAT)



                        hrly_df= pd.DataFrame({'dateTime' :[t], 'field id': [id_], 'HourlyPrecipIn': [aPreVJ],'HourlyRH' : [aHumidVJ], 'HourlyTempF' : [aTempVJ]})

Tags: idconfigfielddatetimetimehrresplon

热门问题