gpt4 book ai didi

python - 将 pandas DataFrame() 拆分为多列的简洁方法

转载 作者:行者123 更新时间:2023-11-30 22:20:03 25 4
gpt4 key购买 nike

如果某个地方存在这种情况,我深表歉意 - 我找不到正确的关键字。

我有一个非常简单的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..

现在我有一些像这样的丑陋的东西(有效):

tmp1 = articles.loc[::3].reset_index(); del tmp1['index'];
tmp1.columns = ['article1_id', 'article1']
tmp2 = articles.loc[1::3].reset_index(); del tmp2['index'];
tmp2.columns = ['article2_id', 'article2']
tmp3 = articles.loc[2::3].reset_index(); del tmp3['index'];
tmp3.columns = ['article3_id', 'article3']

pd.concat([tmp1, tmp2, tmp3], axis=1, ignore_index=False).head()

但我确信 pandas 提供了更干净的东西......

最佳答案

我认为我们正在寻找 array.reshape()

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..

.to_csv():

,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..,,

关于python - 将 pandas DataFrame() 拆分为多列的简洁方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48932870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com