Python 3.x 提取包含()的字符串并填充()中的单词列表

Python 3.x 提取包含()的字符串并填充()中的单词列表,python-3.x,pandas,Python 3.x,Pandas,我在一个dataframe中有几个列,其中一个特定的列和一个示例行子集如下所示。 我想创建列中所有角色的列表 如果dataframe列如下所示 **Roles** Bad Good (UK) Plc (Lead actor) CQWR Luxembourg SA [BIL] (actor, Producer) YZ PQR Ltd (Sponsor) ABCDSA (Actress, Sponsor, Producer, Writer) 然后列表应该是在整个数据框架中看到的唯一角色 [Lead

我在一个dataframe中有几个列,其中一个特定的列和一个示例行子集如下所示。 我想创建列中所有角色的列表

如果dataframe列如下所示

**Roles**
Bad Good (UK) Plc (Lead actor)
CQWR Luxembourg SA [BIL] (actor, Producer)
YZ PQR Ltd (Sponsor)
ABCDSA (Actress, Sponsor, Producer, Writer)
然后列表应该是在整个数据框架中看到的唯一角色

[Lead actor, actor, Producer, Sponsor, Actress, Writer]
因此,拆分基本上是从右侧开始的,在左括号中。
然后我必须确保列表是唯一的

这里,我假设我们考虑的是仅在最后一列中的值

values = df["Roles"].tolist()

new_dict = {}
for el in values:
    el = el.rsplit("(", 1)[-1].strip(")")
    for el2 in el.split(", "):
        new_dict[el2] = 1

unique_list = list(new_dict.keys())
行=[
“Bad Good(英国)有限公司(主角)”,
“CQWR卢森堡公司(演员、制片人)”,
“YZ PQR有限公司(赞助商)”,
ABCDSA(演员、赞助商、制片人、作家)
]
uniqueRoles=[]
seenRoles=set()
对于行中的行:
roles=row.rsplit(“”[-1]。rsplit(“”)[0]
eachRoles=roles.split(“,”)
对于每个角色:
如果不是seenRoles中的角色:
uniqueRoles.append(角色)
seenRoles.add(角色)
打印(uniqueRoles)
输出:

['Lead actor', 'actor', 'Producer', 'Sponsor', 'Actress', 'Writer']