将行添加到带有列的空数据框 [英] adding rows to empty dataframe with columns

查看:43
本文介绍了将行添加到带有列的空数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Pandas,并希望将行添加到已建立列的空DataFrame中.

I am using Pandas and want to add rows to an empty DataFrame with columns already established.

到目前为止,我的代码看起来像这样...

So far my code looks like this...

def addRows(cereals,lines):
    for i in np.arange(1,len(lines)):
        dt = parseLine(lines[i])
        dt = pd.Series(dt)
        print(dt)
    # YOUR CODE GOES HERE (add dt to cereals)
       cereals.append(dt, ignore_index = True)
    return(cereals)

但是,当我跑步时...

However, when I run...

cereals = addRows(cereals,lines)
cereals

数据框返回没有行,只有列.我不确定自己在做什么错,但是我很确定它与append方法有关.有人对我在做什么错有任何想法吗?

the dataframe returns with no rows, just the columns. I am not sure what I am doing wrong but I am pretty sure it has something to do with the append method. Anyone have any ideas as to what I am doing wrong?

推荐答案

您的代码未按预期运行的原因可能有两个:

There are two probably reasons your code is not operating as intended:

  • cereals.append(dt, ignore_index = True)并没有按照您的想象做.您正在尝试附加一个系列,而不是那里的一个DataFrame.

  • cereals.append(dt, ignore_index = True) is not doing what you think it is. You're trying to append a series, not a DataFrame there.

cereals.append(dt, ignore_index = True)不会在原位修改cereals,因此当您返回它时,您将返回未更改的副本.等效函数如下所示:

cereals.append(dt, ignore_index = True) does not modify cereals in place, so when you return it, you're returning an unchanged copy. An equivalent function would look like this:

-

>>> def foo(a):
...    a + 1
...    return a
... 
>>> foo(1)
1

我尚未在计算机上对此进行测试,但我认为您的固定解决方案如下所示:

I haven't tested this on my machine, but I think you're fixed solution would look like this:

def addRows(cereals, lines):
    for i in np.arange(1,len(lines)):
        data = parseLine(lines[i])
        new_df = pd.DataFrame(data, columns=cereals.columns)
        cereals = cereals.append(new_df, ignore_index=True)
    return cereals

顺便说一句..我真的不知道线是从哪里来的,但是我至少会立即对其进行修改,使其看起来像这样:

by the way.. I don't really know where lines is coming from, but right away I would at least modify it to look like this:

data = [parseLine(line) for line in lines]
cereals = cereals.append(pd.DataFrame(data, cereals.columns), ignore_index=True)

如何添加额外的内容排到熊猫数据框

您还可以创建一个新的DataFrame并将该DataFrame附加到现有的DataFrame上.例如

You could also create a new DataFrame and just append that DataFrame to your existing one. E.g.

>>> import pandas as pd
>>> empty_alph = pd.DataFrame(columns=['letter', 'index'])
>>> alph_abc = pd.DataFrame([['a', 0], ['b', 1], ['c', 2]], columns=['letter', 'index'])
>>> empty_alph.append(alph_abc)
  letter  index
0      a    0.0
1      b    1.0
2      c    2.0

正如我在链接中所述,您还可以在DataFrame上使用loc方法:

As I noted in the link, you can also use the loc method on a DataFrame:

>>> df = empty_alph.append(alph_abc)
>>> df.loc[df.shape[0]] = ['d', 3]  // df.shape[0] just finds next # in index
  letter  index
0      a    0.0
1      b    1.0
2      c    2.0
3      d    3.0

这篇关于将行添加到带有列的空数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆