在 for 循环中使用 pandas .append [英] Using pandas .append within for loop
问题描述
我在 for 循环中将行附加到 Pandas DataFrame,但最后数据框始终为空.我不想将行添加到数组然后调用 DataFrame 构造函数,因为我的实际 for 循环处理大量数据.我也试过 pd.concat
没有成功.任何人都可以突出显示我缺少什么以使 append 语句起作用吗?这是一个虚拟示例:
I am appending rows to a pandas DataFrame within a for loop, but at the end the dataframe is always empty. I don't want to add the rows to an array and then call the DataFrame constructer, because my actual for loop handles lots of data. I also tried pd.concat
without success. Could anyone highlight what I am missing to make the append statement work? Here's a dummy example:
import pandas as pd
import numpy as np
data = pd.DataFrame([])
for i in np.arange(0, 4):
if i % 2 == 0:
data.append(pd.DataFrame({'A': i, 'B': i + 1}, index=[0]), ignore_index=True)
else:
data.append(pd.DataFrame({'A': i}, index=[0]), ignore_index=True)
print data.head()
Empty DataFrame
Columns: []
Index: []
[Finished in 0.676s]
推荐答案
您需要将变量 data
设置为等于附加的数据框.与 python 列表上的 append
方法不同,pandas append
不会就地发生
You need to set the the variable data
equal to the appended data frame. Unlike the append
method on a python list the pandas append
does not happen in place
import pandas as pd
import numpy as np
data = pd.DataFrame([])
for i in np.arange(0, 4):
if i % 2 == 0:
data = data.append(pd.DataFrame({'A': i, 'B': i + 1}, index=[0]), ignore_index=True)
else:
data = data.append(pd.DataFrame({'A': i}, index=[0]), ignore_index=True)
print(data.head())
A B
0 0 1.0
1 2 3.0
2 3 NaN
注意:此答案旨在回答提出的问题.然而,这并不是组合大量数据帧的最佳策略.有关更优化的解决方案,请查看下面的亚历山大的回答
NOTE: This answer aims to answer the question as it was posed. It is not however the optimal strategy for combining large numbers of dataframes. For a more optimal solution have a look at Alexander's answer below
这篇关于在 for 循环中使用 pandas .append的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!