Python Pandas使用str.split()方法将字符串拆分为两列

4
如何使用“(”和“)”在数据框中将列中的文本拆分为新的列?
当前数据框:
项目 描述
0 外套 男童(Target)
1 靴子 女士(DSW)
2 袜子 女孩(Kohls)
3 衬衫 男士(Walmart)
4 靴子 女士(DSW)
5 外套 男童(Target)
我想创建的新数据框:
项目 描述 零售商
0 外套 男童 目标
1 靴子 女士 DSW
2 袜子 女孩 Kohls
3 衬衫 男士 沃尔玛
4 靴子 女士 DSW
5 外套 男童 目标

我已经尝试过以下内容:

df[['Description'], ['Retailer']] = df['Description'].str.split("(")

我遇到一个错误:"TypeError: unhashable type: 'list'"
4个回答

2

嗨,我运行了这个小测试,并似乎有效;请注意分割字符串中的空格和\。

import pandas as pd
df = pd.Series(['Boys (Target)','Womens (DSW)','Girls (Kohls)'])
print(df)
d1 = df.str.split(' \(')
print(d1)

1

试试这个:

import pandas as pd

# creating the df
item = ['coat','boots']
dec = ["Boys (Target)", "Womens (DSW)"]
df = pd.DataFrame(item, columns=['Item'])
df['Description'] = dec


def extract_brackets(row):
    return row.split('(', 1)[1].split(')')[0].strip()


def extract_first_value(row):
    return row.split()[0].strip()


df['Retailer'] = df['Description'].apply(extract_brackets)
df['Description'] = df['Description'].apply(extract_first_value)

print(df)

1

你需要在split函数中包含参数expand=True,并重新安排分配两列的方式。考虑使用以下代码:

df[['Description','Retailer']]  = df.Description.str.replace(')','',regex=True)\
    .str.split('(',expand=True)

print(df)

    Item Description Retailer
0   coat       Boys    Target
1  boots     Womens       DSW
2  socks      Girls     Kohls
3  shirt       Mens   Walmart
4  boots     Womens       DSW
5   coat       Boys    Target

我首先从“Description”中删除了闭合括号,然后根据开放括号进行扩展。


0

使用 pandas.Series.str.findall 的一种方法:

df[["Description", "Retailer"]] = df["Description"].str.findall("\w+").apply(pd.Series)
print(df)

输出:

    Item Description Retailer
0   coat        Boys   Target
1  boots      Womens      DSW
2  socks       Girls    Kohls
3  shirt        Mens  Walmart
4  boots      Womens      DSW
5   coat        Boys   Target

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接