简体   繁体   English

在熊猫中追加数据框

[英]Appending data frames in Pandas

I have a 'for' loop that is calling a function (y) on each iteration. 我有一个“ for”循环,每次循环都调用一个函数(y)。 The function returns a 5 column by ten row dataframe called phstab. 该函数返回5列乘10行的数据帧,称为phstab。

for j in cycles
    phstab=y(j)

The last column in the dataframe is the only one that changes. 数据框中的最后一列是唯一更改的列。 The value in the last column is the value for cycles. 最后一列中的值是循环的值。 All the other values in the other columns all stay the same on each iteration. 其他列中的所有其他值在每次迭代中均保持不变。 So if the loop iterates for time for example, it will produce four separate instances of phstab; 因此,例如,如果循环迭代时间,它将产生四个单独的phstab实例; each instance with a different value of cycles. 每个实例具有不同的周期值。

I'd like to append phstab on each iteration so so the output is just one long dataframe instead of four instances. 我想在每次迭代后附加phstab,因此输出只是一个长数据帧,而不是四个实例。 I tried inserting the following statement in the loop but it didn't work 我尝试在循环中插入以下语句,但是没有用

phstab=phstab.append(phstab)

How do I get one single dataframe instead of four separate instances ? 如何获得一个数据框而不是四个单独的实例?

I'm assuming your y(j) returns something like this: 我假设您的y(j)返回如下内容:

In [35]: def y(j):
    ...:     return pd.DataFrame({'a': range(10), 
    ...:                          'b': range(10), 
    ...:                          'c': range(10), 
    ...:                          'd': range(10), 
    ...:                          'e_cycle' : j})

To iterate over this function, adding columns for each iterations, I'd do something like this. 要遍历此函数,为每次迭代添加列,我会做类似的事情。 On the first pass, the dataframe is just set to phstab. 第一次将数据帧设置为phstab。 On each subsequent iteration, a new column is added to phstab based on results of y(j). 在每个后续迭代中,根据y(j)的结果将新列添加到phstab。

I'm assuming you need to rename columns, if y(j) returns a unique column based on the value of j, you'll have to modify to fit. 我假设您需要重命名列,如果y(j)基于j的值返回唯一列,则必须进行修改以适合。

In [38]: cycles = range(5)

In [38]: for i,j in enumerate(cycles):
    ...:     if i == 0:
    ...:         phstab = y(j)
    ...:         phstab = phstab.rename(columns = {'e_cycle' : 'e_' + str(j)})
    ...:     else:
    ...:         phstab['e_' + str(j)] = y(j)['e_cycle']

In [38]: phstab
Out[38]: 
   a  b  c  d  e_0  e_1  e_2  e_3  e_4
0  0  0  0  0    0    1    2    3    4
1  1  1  1  1    0    1    2    3    4
2  2  2  2  2    0    1    2    3    4
3  3  3  3  3    0    1    2    3    4
4  4  4  4  4    0    1    2    3    4
5  5  5  5  5    0    1    2    3    4
6  6  6  6  6    0    1    2    3    4
7  7  7  7  7    0    1    2    3    4
8  8  8  8  8    0    1    2    3    4
9  9  9  9  9    0    1    2    3    4

[10 rows x 9 columns]

Edit: Thanks for clarifying. 编辑:感谢您的澄清。 To have the output in long format, you can you pd.concat, as below. 要以长格式输出,可以如下所示pd.concat。

In [47]: pd.concat([y(j) for j in cycles], ignore_index=True)
Out[47]: 
    a  b  c  d  e_cycle
0   0  0  0  0        0
1   1  1  1  1        0
2   2  2  2  2        0
3   3  3  3  3        0
4   4  4  4  4        0
5   5  5  5  5        0
6   6  6  6  6        0
7   7  7  7  7        0
8   8  8  8  8        0
9   9  9  9  9        0
10  0  0  0  0        1
11  1  1  1  1        1
.....

[50 rows x 5 columns]

I believe a very simple solution is 我相信一个非常简单的解决方案是

my_dataframes = []
for j in cycles:
    phstab = y(j)
    my_dataframes.append(phstab)
full_dataframe = pd.concat(my_dataframes)

Alternatively and more concisely (credit to @chrisb above) 另一种方法和更简洁的方法(上面的@chrisb贷记)

full_dataframe = pd.concat([y(j) for j in cycles], ignore_index=True)

pd.concat merges a list of dataframes together vertically. pd.concat将数据帧列表垂直合并在一起。 Ignoring the index is important so that the merged version doesn't retain the indices of the individual dataframes - otherwise you might end up with an index of [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3] when instead you'd want [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]. 忽略索引很重要,这样合并后的版本就不会保留各个数据框的索引-否则,您最终可能会得到[0、1、2、3、0、1、2、3、0、1的索引,2、3、0、1、2、3],而您却想要[0、1、2、3、4、5、6、7、8、9、10、11、12、13、14, 15]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM