熊猫数据框覆盖同一行

时间:2021-07-28 01:17:53

标签: python pandas dataframe

我正在尝试通过 api 从网站检索每个设备的数据,并将每个关联开发的所有数据保存在一个 csv 文件中

但是我写的循环有问题,它会覆盖信息并且只显示一行输出

(尽管当我尝试打印它以查看输出时,它显示了所有记录!但由于某种原因不存储这些记录)

这是我在下面的尝试

for col_name, data in all_device_ids.items():
data # single device id at a time (colums for all_device_ids is "id" only)
for devid in data: # looping on all device ids to make a request for each of them
    try: 
        request = requests.get("https://confidential.com/event/events?type=LocationUpdate&source="+devid+"&withSourceAssets=true&withSourceDevices=true&dateFrom=2021-07-17T00:00:00.00Z&dateTo=2021-07-18T00:00:00.00Z&pageSize=1&currentPage=1", auth=(user,passwd),headers=headers).json()
        df_nested_list = pd.json_normalize(request, record_path =['events'])
        full = df_nested_list.dropna(axis=1)
        full.columns
        focus = full[["id","creationTime","type"]]
        focus
        #print(focus)
        focus.to_csv('eventsdata.csv', index=False,encoding="utf-8-sig") 
#dropped = focus.drop_duplicates('id', keep='last')

    except:
        'no events for this device'

输出只能是一行!!当每个请求在循环中发布时,它会更新/覆盖该值

1 个答案:

答案 0 :(得分:0)

问题在于“to_csv”方法中使用的“mode”的默认设置。 模式默认设置为“w”。在你的情况下,我们应该追加,所以它应该明确设置为“a”

focus.to_csv('eventsdata.csv', index=False,encoding="utf-8-sig",mode='a')