python - 在 dataframe 上使用 .apply 从一列生成 3 列

我想从每一行中提取一些数据,并制作现有或新 dataframe 的新列,而无需重复执行相同的 re 操作。匹配。

以下是 dataframe 的一个条目的外观:

00:00 Someones_name: some text goes here

我有一个正则表达式,它成功地接受了我需要的 3 个组:

re.match(r"^(\d{2}:\d{2}) (.*): (.*)$", x)

我遇到的问题是,如何在不实际匹配每个新列的情况下采用matched_part[1]、[2] 和[3]。

我不想要的解决方案是:

new_df['time'] = old_df['text'].apply(function1)`
new_df['name'] = old_df['text'].apply(function2)`
new_df['text'] = old_df['text'].apply(function3)`

def function1(x):
  return re.match(r"^(\d{2}:\d{2}) (.*): (.*)$", x)[1]

回答1

您可以将 https://pandas.pydata.org/docs/reference/api/pandas.Series.str.extract.html 与您的模式一起使用

df[['time','name', 'text']] = df['col1'].str.extract(r"^(\d{2}:\d{2}) (.*): (.*)$")
print(df)
#                                        col1   time           name  \
# 0  00:00 Someones_name: some text goes here  00:00  Someones_name   

#                   text  
# 0  some text goes here

相似文章

随机推荐

最新文章