回复:删除括号中的字符串及其空格 [英] re: remove string in brackets and its whitespace

查看:32
本文介绍了回复:删除括号中的字符串及其空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除括号及其内容以及字符串中的尾随空格的最佳re 方法是什么?请注意,并非每个字符串的格式都相同.

what's the best re way to remove brackets and their content, as well as the trailing whitespace within a string? Note that not every string is formatted equally.

脚本:

import pandas as pd
import re

df = pd.DataFrame({'name':
          ['University of Southampton (UK)', 
          'The College of William and Mary', 
          'University of Reading (UK)', 
          'Queensland University (Australia)']})

def cleaning(text):
    cleaned = re.findall(re.compile('^([^,]+).+'), text)
    cleaned = re.findall(re.compile('\(.*\)'), str(cleaned)) # Why do I have to str() here btw?
    return cleaned

df['name'].apply(lambda x: cleaning(x))

退货:

0    []
1    []
2    []
3    []

所需的输出(末尾没有空格):

0    University of Southampton
1    The College of William and Mary
2    University of Reading
3    Queensland University

感谢您的帮助!

推荐答案

仅适用于这种特定情况,但您可以这样做

Only work for this specific case, but you can do

df.name.str.split('\(',expand=True)[0].str.strip()

这篇关于回复:删除括号中的字符串及其空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆