简体   繁体   English

to_csv 仅保存最后一次出现(python)

[英]to_csv saves only the last occurence (python)

I have a df resulting from a loop (which is part of a longer program that's why I'm not posting it).我有一个循环产生的 df (这是一个较长程序的一部分,这就是我不发布它的原因)。

If I use print I get the whole df if I use write I get the whole df if I use to_csv I get only the last occurence (regardless the indentation)如果我使用 print 我会得到整个 df 如果我使用 write 我会得到整个 df 如果我使用 to_csv 我只会得到最后一次出现(不管缩进)

Thank you for the help感谢您的帮助

Edit: code added编辑:添加代码

j = range(14)
selections = []
for ind in j:
    selections.append(files[ind])
    for selection in selections:
        first_classes = selection.find('ul', class_='first_class')
        second_classes= first_class.find_all('li', class_='second_class')
        list = []
    for second_class in second_classes:
        name = second_class.find('a').text
        list.append(name)
    i = range(5)
    h = []
    for index in i:
        h.append(list[index])
    df = pd.DataFrame(h)
    df.to_csv('myfile.csv', index=False, header=False)

您需要在它之前的循环中获取最后一个for循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM