Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 拆分字符串的有效方法,其中单个单词由大写字母或数字分隔,并将其应用于整个数据帧列_Python_Regex_Pandas_Dataframe - Fatal编程技术网

Python 拆分字符串的有效方法,其中单个单词由大写字母或数字分隔,并将其应用于整个数据帧列

Python 拆分字符串的有效方法,其中单个单词由大写字母或数字分隔,并将其应用于整个数据帧列,python,regex,pandas,dataframe,Python,Regex,Pandas,Dataframe,我有一个数据框,其中团队名称以字符串形式给出。拆分字符串以获取单个团队名称的规则如下 每个大写的角色都会组建一个新团队 每个数字字符都会启动一个新字符串,除非前一个 第一个非数字或非字母字符后没有团队名称 我的解决方案如下。这是可行的,但我正在寻找改进的方法 整个拆分可以作为reg ex完成吗 任何比使用更快速的方法都适用。我已经看到了一些关于使用并行版本加速应用程序的注释,但不知道是否有更快的方法来实现这一点 也许可以试着把这个贴在代码审查上?谢谢@Chris。上述解决方案的一个问题是,它不满

我有一个数据框,其中团队名称以字符串形式给出。拆分字符串以获取单个团队名称的规则如下

  • 每个大写的角色都会组建一个新团队
  • 每个数字字符都会启动一个新字符串,除非前一个
  • 第一个非数字或非字母字符后没有团队名称
  • 我的解决方案如下。这是可行的,但我正在寻找改进的方法

  • 整个拆分可以作为reg ex完成吗
  • 任何比使用更快速的方法都适用。我已经看到了一些关于使用并行版本加速应用程序的注释,但不知道是否有更快的方法来实现这一点

  • 也许可以试着把这个贴在代码审查上?谢谢@Chris。上述解决方案的一个问题是,它不满足解析规则#3
    在第一个非数字或非字母字符后没有团队名称
    ,例如,在下面的示例中,``df=pd.DataFrame({'Population':[1400,100,1000],'Teams':['yankesmetsbrooklyn[]Dontpick“,”Rays“,”GiantsAthletics49ers“,”Ad Market“:[1040020008400]},索引=[“纽约地区”,“坦帕湾”,“旧金山湾区])```
    import pandas as pd
    import numpy as np
    import re
    
    def splitDfCap(next_row):
        new_team = ''
        team_list = []
        last_char_num = False
        for next_char in next_row.Teams:
            stop_current = re.match('[A-Z]', next_char) or (re.match('[0-9]', next_char) and not(last_char_num))
            if stop_current and len(new_team) > 0:
                team_list.append(new_team)
                new_team = ''
            
            if re.match('[a-zA-Z0-9]', next_char):
                new_team = new_team + next_char
                last_char_num = re.match('[0-9]', next_char)
            else:
                break
        
        if len(new_team) > 0:
            team_list.append(new_team)
                           
        return team_list
            
    
    #Setup
    df = pd.DataFrame({'Population' : [1400, 100, 1000], 
                       'Teams' : ['YankeesMetsBrooklyn[]', 'Rays', 'GiantsAthletics49ers'], 
                       'Ad Market' : [10400, 2000, 8400]},
                      index = ['New York Area', 'Tampa Bay', 'SF Bay Area'])
    
    #Create list of teams and then explode
    df['Team List'] = df.apply(splitDfCap, axis='columns')
    df = df.rename_axis(index='Region')\
           .explode('Team List')\
           .rename(columns={'Team List': 'Team'})\
           .drop(columns=['Teams'])
    print(df)
    
    Output is
                   Population  Ad Market       Team
    Region                                         
    New York Area        1400      10400    Yankees
    New York Area        1400      10400       Mets
    New York Area        1400      10400   Brooklyn
    Tampa Bay             100       2000       Rays
    SF Bay Area          1000       8400     Giants
    SF Bay Area          1000       8400  Athletics
    SF Bay Area          1000       8400      49ers
    
    import pandas as pd
    df = pd.DataFrame({'Population' : [1400, 100, 1000], 
                       'Teams' : ['YankeesMetsBrooklyn[]', 'Rays', 'GiantsAthletics49ers'], 
                       'Ad Market' : [10400, 2000, 8400]},
                      index = ['New York Area', 'Tampa Bay', 'SF Bay Area'])
    
    df['Teams']=df.Teams.str.findall('(?:[A-Z]|\d+)[^A-Z\d+\W]*')
    df = df.explode('Teams')
    df.reset_index(inplace=True)
    df.columns = ['Region','Population','Ad Market','Team']