首页 > 解决方案 > 在 pandas python 列中追加循环输出

问题描述

我正在使用下面的代码将输出附加到空数据帧

image_data = pd.DataFrame()

for i in words:
    y = re.findall('{} ([^ ]*)'.format(re.escape(i)), data)
    x = y
    image_data = image_data.append(x, ignore_index = True)

我得到如下输出,但我想要

        0
0   30708
1      15
2    1800
0   19200
1      50
2    1180

我想要的输出是什么

        0    1       2
0   30708   15    1800
1   19200   50    1180

每次循环重复时,我怎样才能使 3 行到 3 列。

标签: pythonpandasdataframe

解决方案


如果x是值列表,请使用:

image_data = image_data.append([x], ignore_index = True)

将所有值追加为新行,而不是将单个元素追加为行。在此处查看有关 append 方法的更多详细信息。


推荐阅读