Python中文
首页
教程
问答
标签
搜索
登录
注册
Pandas dataframe只将最后一行写入.cs
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我想将这些数据帧写入<code>csv</code>文件,但是我只写最后一行。我的代码的目标是调用一个API,并获得给定lat/lng“昨天”的最高温度。在</p> <p>我目前的输出是:</p> <p><img src="https://i.stack.imgur.com/JyIpJ.png" alt="Current Output"/></p> <p>我希望我的输出是:</p> <p><img src="https://i.stack.imgur.com/JOPdB.png" alt="Desired Output"/></p> <p>代码:</p> <pre><code>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)` </code></pre> <p>当我打印<code>i</code>时,我的输出是:</p> <p><img src="https://i.stack.imgur.com/tdkuj.png" alt="printed i"/></p> <p>编辑:当我使用<code>mode= 'a'</code>时 以下是我的输出:</p> <p><a href="https://i.stack.imgur.com/TVNar.png" rel="nofollow noreferrer">When mode a is used.</a></p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>尝试使用附加模式:</p> <pre class="lang-py prettyprint-override"><code>i.to_csv(output_high_csv, encoding='utf-8', index=True, mode='a') </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何添加虚拟方法
6 回答
如何添加表示整数的擦边字符串?
4 回答
如何添加要在Bokeh中使用的新font.ttf文件?
2 回答
如何添加要显示的矩阵XY轴编号和XY轴
8 回答
如何添加计数?
6 回答
如何添加计数器函数?
10 回答
如何添加计数器列来计算数据帧中另一列中的特定值?
6 回答
如何添加计数器来跟踪while循环中的月份和年份?
6 回答
如何添加计数并删除countplot的顶部和右侧脊椎?
6 回答
如何添加计时器wx.应用程序更新窗口对象的主循环?
6 回答
如何添加评论到帖子?PostDetailVew,Django 2.1.5
2 回答
如何添加评论拉梅尔亚姆
2 回答
如何添加诸如矩阵Python/Pandas之类的数据帧?
7 回答
如何添加谷歌地点自动完成到Flask?
2 回答
如何添加超时、python discord bot
4 回答
如何添加超过1dp的检查
3 回答
如何添加距离方法
10 回答
如何添加跟随游戏的敌人精灵
10 回答
如何添加路径以便python可以找到程序?
4 回答
如何添加身份验证/安全性以使用happybase访问HBase?
8 回答