熊猫数据框仅将最后一行写入.csv

时间:2019-04-29 15:02:42

标签: python pandas

我想将这些数据帧写到csv文件中,但是我只写最后一行。我的代码的目的是调用一个API并针对给定的经度/经度获得“昨天”的最高温度。

我当前的输出是:

Current Output

我希望输出为:

Desired Output

代码:

import pandas as pd
import requests
import csv
import json
from datetime import datetime, timedelta
from pandas import DataFrame

#read csv

output_high_csv = r"C:\wab\outputempbeets1.csv"

se_ = requests.Session()

df = pd.read_csv(csvdata)
df = df [['lat', 'lon', 'field id']]

#convert Pandas series to string
dfText = df.astype(basestring)

#get lat/lng from dataframe
dfLat = dfText['lat']
dfLon = dfText['lon']
dfFid = dfText['field id']

#get values from series
latval = list(dfLat.values)
lonval = list(dfLon.values)
idVal = list(dfFid.values)
dayVal = datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')

#Zip Field ID, Lat, Lng

for latrow, lonrow, idVal in zip(latval, lonval, idVal):
    #send lat/lngs to DTN
    url = 'https://insight.api.wdtinc.com/daily-high-temperature/' + str(latrow )+'/' + str(lonrow) +'?start='+ dayVal +'T00:00:00Z&end=' +dayVal+'T01:00:00Z'+ '&unit=fahrenheit'
    r = se_.get(url, auth=('USERNAME', 'PASSWORD'), timeout= 10)
    # print(r.url)
    hiTemp = (json.loads(r.content)['series'][0]['value'])
    strTemp = str(hiTemp)
    tempDF = {'field id': [idVal],
        'high temperature': [strTemp]
        }
    df = DataFrame(tempDF,columns= ['field id', 'high temperature'])    
    dfList = [df]
    dfs = [df.set_index('field id') for df in dfList]
    i = pd.concat(dfs, axis=1)
    print i
    i.to_csv(output_high_csv, encoding='utf-8', index=True)`

当我打印i时,输出为:

printed i

编辑:当我使用mode= 'a'时 这是我的输出:

When mode a is used.

2 个答案:

答案 0 :(得分:0)

尝试使用附加模式:

i.to_csv(output_high_csv, encoding='utf-8', index=True, mode='a')

答案 1 :(得分:0)

您的代码非常复杂。我认为这是您正在尝试的简化版本。它可能有错误,但是如果这不能回答您的问题,请尝试并使用它来简化问题中的代码。

import pandas as pd
import requests
import csv
import json
from datetime import datetime, timedelta
from pandas import DataFrame
from pathlib import Path

DATA_PATH = Path('C:/wab')
input_csv = 'my_data.csv'
output_csv = 'outputempbeets1.csv'

se_ = requests.Session()

df = pd.read_csv(DATA_PATH / input_csv,
                 usecols=['lat', 'lon', 'field id'],
                 dtype=str)

dayVal = datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')
dfs = []

def get_high_temp(lat, lon, dayVal):
    url = f'https://insight.api.wdtinc.com/daily-high-temperature/{lat}/{lon}'
    params = {
        'start': f'{dayVal}T00:00:00Z',
        'end': f'{dayVal}T01:00:00Z',
        'unit': 'fahrenheit',
    }
    response = se_.get(url, 
                       params=params, 
                       auth=('USERNAME', 'PASSWORD'),
                       timeout=10)

    hi_temp = str(json.loads(response.content)['series'][0]['value'])
    return hi_temp

for _, lat, lon, id_ in df.itertuples():
    hi_temp = get_high_temp(lat, lon, dayVal)
    temp_df = pd.DataFrame({'high temperature': [hi_temp],},
                           index=[id_])    
    dfs = dfs.append(temp_df)

pd.concat(dfs).to_csv(DATA_PATH / output_csv, encoding='utf-8')