本文介绍了保持标题,同时附加到Pandas数据框w / Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我解析许多文件中包含的数据,循环遍历它们并将某些元素存储在列表中,并使用Python将每个生成的列表附加到Pandas的数据框中。

So I am parsing data contained in many files, looping through them and storing certain elements in a list, and appending each resulting list to a dataframe with Pandas using Python.

它的工作,除了我无法弄清楚如何保持头行添加。它不会消失或与每个附件重复。

It works, except I can't figure out how to keep the header row while appending. It either disappears or is duplicated with each append.

以下代码作为最新代码的示例:

The below code serves as an example of the latest code:

import pandas as pd

for i in range(1,4):
    data = [{'name': 'Company'+str(i), 'city': 'New York'}]

    stuff = []
    for element in data:
        stuff.append(element)

    df = pd.DataFrame(columns=["name",
                               "city"])

    for record in stuff:
        df = df.append(record, ignore_index=True)

    df.to_csv('test.csv', mode='a', header=False, index=False)

使用此代码,输出(csv文件)是:

With this code the output (csv file) is:

Company1    New York
Company2    New York
Company3    New York

但我正在寻找输出为:

name        city
Company1    New York
Company2    New York
Company3    New York

谢谢!

推荐答案

但是你设置了 header = False

df.to_csv('test.csv', mode='a', header=False, index=False)

你应该做:

df.to_csv('test.csv', mode='a', header=True, index=False)

您只需将其设置为 True 为第一次迭代,然后 False 为后续迭代

You just need to set it to True for the first iteration and then False for the subsequent iterations

基本上您只需执行以下操作:

Basically you just do something like the following:

# add this outside your for loop
writeHeader = True

# use the following where you write to csv
if writeHeader is True:
    df.to_csv('test.csv', mode='a', header=True, index=False)
    writeHeader = False
else:
    df.to_csv('test.csv', mode='a', header=False, index=False)

或类似

所以完整的东西看起来像:

So the complete thing looks like:

import pandas as pd
writeHeader = True

for i in range(1,4):
    data = [{'name': 'Company'+str(i), 'city': 'New York'}]

    stuff = []
    for element in data:
        stuff.append(element)

    df = pd.DataFrame(columns=["name",
                               "city"])

    for record in stuff:
        df = df.append(record, ignore_index=True)

    if writeHeader is True:
        df.to_csv('test.csv', mode='a', header=True, index=False)
        writeHeader = False
    else:
        df.to_csv('test.csv', mode='a', header=False, index=False)

这篇关于保持标题,同时附加到Pandas数据框w / Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 07:25