【问题标题】:How to extract particular values from a dataframe col and put them in another column?如何从数据框 col 中提取特定值并将它们放在另一列中?
【发布时间】:2020-11-18 02:12:17
【问题描述】:

我有一个由一列字符串组成的数据框。我想从一列中提取地点、日期和比赛编号。

数据框:

- S.no.            FileName
- 0.     Albany17NOV19sectionalRace1.csv
- 1.     Albany22NOV19sectionalRace4.csv
- 2.     New York1NOV19sectionalRace7.csv
- 3.     Aquis Park Gold Coast27NOV19sectionalRace2.csv 

所需的数据框:

- S.no.   Place                     Date     Racenumber
- 0.     Albany                    17NOV19    Race1
- 1.     Albany                    22NOV19    Race4
- 2.     New York                  1NOV19     Race7
- 3.     Aquis park Gold Coast     27NOV19    Race2

【问题讨论】:

    标签: python pandas dataframe data-extraction


    【解决方案1】:

    拆分;

    1. digit 后跟 Nondigitdigit17NOV19

    1. sectional

    3 个特殊字符.

    拆分后删除所有具有 None 作为值的行和任何其他不需要的行。如果需要,可以重命名列

        df=df.FileName.str.split('(\d+\D+\d+)|(sectional)|(\.)', expand=True).dropna(1).drop(columns=[4,6,11,12])
    print(df)
            
    
              
    
                        0        1      8
    0                 Albany  17NOV19  Race1
    1                 Albany  22NOV19  Race4
    2               New York   1NOV19  Race7
    3  Aquis Park Gold Coast  27NOV19  Race2
    

    【讨论】:

    • 这有点用,但是正如您在示例中看到的那样,日期被划分为 11 月 17 日在 Albany1 和 7Nov19 中被划分。
    • 我不好。做了eidts。基本上忘了把+放在第一个\d
    • 如果可以,请告诉我,或者需要更多帮助。乐于助人
    • 效果很好!非常感谢队友。我只需要你的建议,我可以在哪里学习这些表达式 '(\d+\D+\d+)|(section)|(\.)' 。
    • 当我得到一个包含像这样的名称 [6 MISS CIRCLEWORK (3)] 的列时,我想提取第一个数字和最后一个数字,在这种情况下将是“6”和“3” .
    【解决方案2】:

    一个正则表达式函数应该可以完成这项工作:

    import re
    
    
    def split_string_to_groups(s: str):
        temp = re.compile("([a-zA-Z\s]+)([0-9]+[a-zA-Z]+[0-9]+)(sectional)(Race[0-9]+)(\.csv)")
        res = temp.match(s).groups()
        return res
    
    print(split_string_to_groups("Albany17NOV19sectionalRace1.csv"))
    print(split_string_to_groups("Aquis Park Gold Coast27NOV19sectionalRace2.csv"))
    

    输出:

    ('Albany', '17NOV19', 'sectional', 'Race1', '.csv')
    ('Aquis Park Gold Coast', '27NOV19', 'sectional', 'Race2', '.csv')
    

    【讨论】:

      【解决方案3】:

      不如其他答案好,但它仍然可以完成工作:

      extract_info = {
          'Date': lambda x: x.str.findall('\d.+?\d{2}').str[0],
          'Place': lambda x: x.str.findall('^.+?(?=\d)').str[0],
          'Racenumber': lambda x: x.str.findall('Race\d+').str[0]}
      
      df = df.FileName.agg(extract_info.values())
      df.columns = extract_info.keys()
      
      print(df)
      
            Date                  Place Racenumber
      0  17NOV19                 Albany      Race1
      1  22NOV19                 Albany      Race4
      2   1NOV19               New York      Race7
      3  27NOV19  Aquis Park Gold Coast      Race2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-17
        • 2021-01-04
        • 2018-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多