Python 将DataFrame()拆分为多列的干净方法

Python 将DataFrame()拆分为多列的干净方法,python,pandas,Python,Pandas,我很抱歉,如果这存在的话--我找不到正确的关键字 我有一个非常简单的pd.DataFrame()如下所示 articles = pd.DataFrame( [(0, "Once upon.."), (1, "It happened.."), (2, "The story.."), (3, "So many.."), (4, "Ho

我很抱歉,如果这存在的话--我找不到正确的关键字

我有一个非常简单的
pd.DataFrame()
如下所示

articles = pd.DataFrame(
                [(0, "Once upon.."),
                 (1, "It happened.."),
                 (2, "The story.."),
                 (3, "So many.."),
                 (4, "How long.."),
                 (5, "It's been..")],
            columns=["article_id", "article"])
以致

>>> articles

    article_id  article
0   0   Once upon..
1   1   It happened..
2   2   The story..
3   3   So many..
4   4   How long..
5   5   It's been..
我只想将该列拆分为3列(无论顺序如何,但假设保持顺序),例如:

    article1_id article1    article2_id article2    article3_id article3
0   0   Once upon.. 1   It happened..   2   The story..
1   3   So many..   4   How long..  5   It's been..
现在,我有一些丑陋的东西,像这样(这是有效的):


但是我确信熊猫提供了更干净的东西…

我想我们正在寻找数组。重塑()

返回:

  article1_id     article1 article2_id       article2 article3_id     article3
0           0  Once upon..           1  It happened..           2  The story..
1           3    So many..           4     How long..           5  It's been..
2           6  It's been..           7    It's been..                           
.to_csv():


在创建数据帧之前,我们可以这样做吗?还是你在找熊猫行动?我想问一下,我们为什么要这样做?似乎完全错了。(以及为什么…,我只是好奇我们是否可以换一种方式思考)而是在创建它之后再这样做——必须有一种快速的方法。我需要创建一个CSV,每个CSV行包含三篇文章。谢谢,我需要的正是
.reforme()
,我不记得了。我想你的意思是在评论之前加上几行?很好的精神检查顺便说一句。@Arnaud是的,我是说行。谢谢
import pandas as pd

df = pd.DataFrame(
                [(0, "Once upon.."),
                 (1, "It happened.."),
                 (2, "The story.."),
                 (3, "So many.."),
                 (4, "How long.."),
                 (5, "It's been.."),
                 (6, "It's been.."),
                 (7, "It's been..")],
            columns=["article_id", "article"])

# New cols (let them define the length of reshape)
cols = ['article1_id','article1','article2_id','article2','article3_id','article3']

# If size of dataframe is not divisable by len(cols) add rows
# Can be removed if certain of length.
while df.size % len(cols) != 0:
    df.loc[len(df)] = ('','')

df = pd.DataFrame(df.values.reshape(df.size//len(cols),len(cols)), columns=cols)

print(df)
  article1_id     article1 article2_id       article2 article3_id     article3
0           0  Once upon..           1  It happened..           2  The story..
1           3    So many..           4     How long..           5  It's been..
2           6  It's been..           7    It's been..                           
,article1_id,article1,article2_id,article2,article3_id,article3
0,0,Once upon..,1,It happened..,2,The story..
1,3,So many..,4,How long..,5,It's been..
2,6,It's been..,7,It's been..,,