pandas 为什么我不能在循环中附加熊猫数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43796646/
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
Why can't I append pandas dataframe in a loop
提问by mcragun
I know that there are several ways to build up a dataframe in Pandas. My question is simply to understand why the method below doesn't work.
我知道有几种方法可以在 Pandas 中构建数据框。我的问题只是为了理解为什么下面的方法不起作用。
First, a working example. I can create an empty dataframe and then append a new one similar to the documenta
首先,一个工作示例。我可以创建一个空的数据框,然后附加一个类似于文档的新数据框
In [3]: df1 = pd.DataFrame([[1,2],], columns = ['a', 'b'])
...: df2 = pd.DataFrame()
...: df2.append(df1)
Out[3]: a b
0 1 2
Out[3]: a b
0 1 2
However, if I do the following df2 becomes None:
但是,如果我执行以下操作 df2 变为 None:
In [10]: df1 = pd.DataFrame([[1,2],], columns = ['a', 'b'])
...: df2 = pd.DataFrame()
...: for i in range(10):
...: df2.append(df1)
In [11]: df2
Out[11]:
Empty DataFrame
Columns: []
Index: []
Can someone explain why it works this way? Thanks!
有人可以解释为什么它会这样工作吗?谢谢!
回答by Rod Manning
This happens because the .append() method returns a new df:
发生这种情况是因为 .append() 方法返回一个新的 df:
pandas.DataFrame.append
Returns:appended:DataFrame
pandas.DataFrame.append
返回:附加:DataFrame
Here's a working example so you can see what's happening in each iteration of the loop:
这是一个工作示例,因此您可以查看循环的每次迭代中发生的情况:
df1 = pd.DataFrame([[1,2],], columns=['a','b'])
df2 = pd.DataFrame()
for i in range(0,2):
print(df2.append(df1))
> a b
> 0 1 2
> a b
> 0 1 2
If you assign the output of .append() to a df (even the same one) you'll get what you probably expected:
如果您将 .append() 的输出分配给 df (即使是相同的),您将得到您可能期望的结果:
for i in range(0,2):
df2 = df2.append(df1)
print(df2)
> a b
> 0 1 2
> 0 1 2
回答by TheManWhoKnows
I think what you are looking for is:
我认为你正在寻找的是:
df1 = pd.DataFrame()
df2 = pd.DataFrame([[1,2,3],], columns=['a','b','c'])
for i in range(0,4):
df1 = df1.append(df2)
df1
回答by labixiaoK
df.append() returns a new object. df2 is a empty dataframe initially, and it will not change. if u do a df3=df2.append(df1), u will get what u want
df.append() 返回一个新对象。df2 最初是一个空数据框,它不会改变。如果你做一个 df3=df2.append(df1),你会得到你想要的