为什么我不能在循环中附加 pandas 数据框 [英] Why can't I append pandas dataframe in a loop
问题描述
我知道有几种方法可以在Pandas中建立数据框.我的问题只是想了解为什么下面的方法不起作用.
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.
首先,是一个工作示例.我可以创建一个空的数据框,然后添加一个类似于documenta的新数据框.
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
但是,如果执行以下操作,则df2会变为None:
However, if I do the following df2 becomes 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!
推荐答案
之所以会发生这种情况,是因为.append()方法返回了新的df:
This happens because the .append() method returns a new df:
pandas.DataFrame.append
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
如果将.append()的输出分配给df(甚至是相同的),则会得到您可能期望的结果:
If you assign the output of .append() to a df (even the same one) you'll get what you probably expected:
for i in range(0,2):
df2 = df2.append(df1)
print(df2)
> a b
> 0 1 2
> 0 1 2
这篇关于为什么我不能在循环中附加 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!