gpt4 book ai didi

python - 为什么只有一行导出到excel?

转载 作者:行者123 更新时间:2023-12-04 21:27:31 27 4
gpt4 key购买 nike

当我尝试将我的 pandas 数据框导出到 excel 时,只导出一行。
数据库是在一个大循环期间创建的,该循环基本上给出了输出:

                                    Names  Average Selling Price
0 ★ Flip Knife | Gamma Doppler (Factory New) 2.418605
Names Average Selling Price
0 ★ Flip Knife | Gamma Doppler (Factory New) 1.69697
Names Average Selling Price
0 ★ Flip Knife | Gamma Doppler (Factory New) 1.0
但在更大的范围内。
import time
import requests
from bs4 import BeautifulSoup
from numpy import mean
import re
import numpy
import statistics
import xlsxwriter
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import os
import pandas as pd
from pandas import ExcelWriter
import pandas as pd




workbook = xlsxwriter.Workbook('JohnsWaxpeerDatabase.xlsx')
worksheet = workbook.add_worksheet()
workbook.close()


item=[]
listo=[]
URL = (
"https://waxpeer.com/api/data/index/?skip=
{offset}&sort=best_deals&game=csgo&all=0"
)

offset = 0

while True:
try:
response = requests.get(URL.format(offset=offset)).json()
for data in response["items"]:
namer=(data["name"])





offset += 50



if offset == 8000:
break


listo.append(namer)
namelist = sorted(set(listo))

for names in namelist:

headers = {'content-type': 'application/json;charset=UTF-8'}

data = {"name":names}

r = requests.post('https://waxpeer.com/api/get-sales-history',
headers=headers, json=data)

history=r.json()

count=[i['count'] for i in history['data']]

counter = [list(map(int, x)) for x in count]

avgn=(numpy.mean(counter))

itemss=(namer, avgn)




itemois=pd.DataFrame({
'Names':[namer],
'Average Selling Price': [avgn]}). dropna()

print(itemois)



except KeyError:
break

itemois.to_excel('JohnsWaxpeerDatabase.xlsx')
按要求提供完整代码,抱歉造成困惑。
当我尝试将数据库导出到 excel 时,它只会添加第一行。
有任何想法吗?

最佳答案

感谢您的评论澄清,这里有两个调整

  • 添加了用于追加行的列表
  • np.means 抛出异常
    因为您在计数器
  • 中有如下列表
    counter=[[3],[1, 1]]
    my_dict_list=[] # added to your code, to append the dictionary
    #break_counter=0
    while True:
    try:
    response = requests.get(URL.format(offset=offset)).json()
    for data in response["items"]:
    namer=(data["name"])
    offset += 50
    if offset == 8000:
    break
    listo.append(namer)
    namelist = sorted(set(listo))
    for names in namelist:

    headers = {'content-type': 'application/json;charset=UTF-8'}

    data = {"name":names}

    r = requests.post('https://waxpeer.com/api/get-sales-history', headers=headers, json=data)
    history=r.json()
    count=[i['count'] for i in history['data']]

    counter = [list(map(int, x)) for x in count]

    try:
    print(counter)
    avgn=(numpy.mean(counter))
    except Exception as e: # new lines added to handle exception
    print(f"Exception:{e} for request {r}") # new lines added to handle exception
    counter= [e1 for e in counter for e1 in e] # new lines added to handle exception, i.e flat the list
    avgn=(numpy.mean(counter)) # new lines added to handle exception


    itemss=(namer, avgn)
    my_dict_list.append({'Names':namer,'Average Selling Price': avgn}) # # added to your code, to append to list

    except KeyError:
    break
    构建数据框和 dropna
    df=pd.DataFrame(my_dict_list)
    df.dropna()
    输出: enter image description here

    关于python - 为什么只有一行导出到excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67352630/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com