将类的属性输入到 Pandas 数据框中

问题描述 投票:0回答:1

我有一个名为 Entry 的类,它具有这些属性;

列、任务、描述、分配的成员、时间线

每个属性存储一个字符串。

我想在数据框的每一行中输入每个属性。

Entry的Column属性用于标识该entry所属的同名列,其余属性则输入到dataframe中。

我遇到的问题是,每当我添加属于新列的新条目时,它都会进入属于不同列的前一个条目之后的索引。我想确保每个新条目都进入该列的下一个可用行。

错误的输出看起来像这样;

索引 第 1 栏 第 2 栏 第3栏 第 4 栏
0 任务:文字 NaN NaN NaN
1 描述:文字 NaN NaN NaN
2 指定成员:文本 NaN NaN NaN
3 时间轴:文字 NaN NaN NaN
4 NaN 任务:文字 NaN NaN
5 NaN 描述:文字 NaN NaN
6 NaN 指定成员:文本 NaN NaN
7 NaN 时间轴:文字 NaN NaN
8 NaN NaN 任务:文字 NaN
9 NaN NaN 描述:文字 NaN
10 NaN NaN 指定成员:文本 NaN
11 NaN NaN 时间轴:文字 NaN
12 NaN NaN NaN 任务:文字
13 NaN NaN NaN 描述:文字
14 NaN NaN NaN 指定成员:文本
15 NaN NaN NaN 时间轴:文字

这是我的初步实现,

import pandas as pd

class Entry:
    def __init__(self, column, task, desc, member, timeline):
        self.column = column
        self.task = f"Task: {task}"
        self.desc = f"Description: {desc}"
        self.member = f"Assigned Member: {member}"
        self.timeline = f"Timeline: {timeline}"
        
#Add each attribute of the entry to the dataframe, use the c attribute as the column name, place each attribute in it's own row
def enter_entries(df, entries):
    for entry in entries:
        df = df._append({entry.column: entry.task}, ignore_index=True)
        df = df._append({entry.column: entry.desc}, ignore_index=True)
        df = df._append({entry.column: entry.member}, ignore_index=True)
        df = df._append({entry.column: entry.timeline}, ignore_index=True)
    return df

# Create the DataFrame
df = pd.DataFrame(columns=['column 1', 'column 2', 'column 3', 'column 4'])

#Backlog Entry 
entry = Entry('column 1', 'text', 'text', 'text', 'text')

#Doing Entry
entry2 = Entry('column 2', 'text', 'text', 'text', 'text')

#Review Entry
entry3 = Entry('column 3', 'text', 'text', 'text', 'text')

#Done Entry
entry4 = Entry('column 4', 'text', 'text', 'text', 'text')

#Add each entry to the entries list
entries = [entry, entry2, entry3, entry4]

df = enter_entries(df, entries)

display(df)

有什么建议吗?

谢谢你:)

python pandas dataframe class
1个回答
0
投票

首先我们创建一个虚拟类来保存数据:

from dataclasses import dataclass
import pandas as pd

@dataclass
class Entry:
    column: str
    task: str = "task"
    desc: str = "description"
    member: str = "member"
    timeline: str = "timeline"

我们创建一些综合条目:

entries = [Entry("Col-1"), Entry("Col-2"), Entry("Col-3"), Entry("Col-4")]

现在迭代条目及其属性就足以更新数据框:

def append_entries(entries, frame=None):
    frame = frame or pd.DataFrame()
    for entry in entries:
        col = entry.column
        for key in entry.__dict__:
            if key != "column":
                frame.loc[key, col] = entry.__dict__[key]
    return frame

以下调用返回:

append_entries(entries)

                Col-1        Col-2        Col-3        Col-4
task             task         task         task         task
desc      description  description  description  description
member         member       member       member       member
timeline     timeline     timeline     timeline     timeline
© www.soinside.com 2019 - 2024. All rights reserved.