pandas 将行添加到带有列的空数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42701570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adding rows to empty dataframe with columns
提问by brianhalperin
I am using Pandas and want to add rows to an empty DataFrame with columns already established.
我正在使用 Pandas 并希望将行添加到已建立列的空 DataFrame 中。
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
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?
数据框返回没有行,只有列。我不确定我做错了什么,但我很确定它与 append 方法有关。有人对我做错了什么有任何想法吗?
回答by Nick Brady
There are two probably reasons your code is not operating as intended:
您的代码未按预期运行可能有两个原因:
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)
does not modifycereals
in place, so when you return it, you're returning an unchanged copy. An equivalent function would look like this:
cereals.append(dt, ignore_index = True)
不是在做你认为的事情。您正在尝试在那里附加一个系列,而不是一个 DataFrame。cereals.append(dt, ignore_index = True)
不会cereals
就地修改,因此当您返回它时,您返回的是未更改的副本。等效的函数如下所示:
--
——
>>> 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)
How to add an extra row to a pandas dataframe
You could also create a new DataFrame and just append that DataFrame to your existing one. E.g.
您还可以创建一个新的 DataFrame 并将该 DataFrame 附加到您现有的 DataFrame 中。例如
>>> 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
As I noted in the link, you can also use the loc
method on a DataFrame:
正如我在链接中指出的,您还可以在loc
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