分隔 pandas 列并存储特定值

Delimit pandas column and store specific values

我有一种情况,我想以某种方式分隔列,如果应该返回信息,则只分隔一定数量的列。 pandas 列具有以下格式的数据:

Turkey (A)- ABC - CDESS - CDS DEE 10. AAAA, || Office 1'

我只想要上面的以下内容:

Office 1

并且此替换必须应用于列中的每个条目。

我该如何实现?

将 pandas string extract 与正则表达式一起使用:正则表达式将搜索 ||,提取值,然后您可以去除任何空格。

text = 'Turkey (A)- ABC - CDESS - CDS DEE 10. AAAA, || Office 1'
df = pd.DataFrame([text])

df['extract'] = df[0].str.extract(r'((?<=\|\|).*)')
df['extract'] = df['extract'].str.strip()
print(df)

       0                                                extract
0   Turkey (A)- ABC - CDESS - CDS DEE 10. AAAA, ||...   Office 1

使用str.split()

text = 'Turkey (A)- ABC - CDESS - CDS DEE 10. AAAA, || Office 1'
df = pd.DataFrame([text])
print(df[0].str.split('\|\|', expand=True)[1])

0     Office 1
Name: 1, dtype: object