【问题标题】:Multiple inserts in numpy where paired elements don't have subtextnumpy中的多个插入,其中配对元素没有潜台词
【发布时间】:2019-07-23 23:53:23
【问题描述】:

这个问题是在@ecortazar 回答的this previous post 上跟进的。但是,我还想在不包含某个字符串的 pd.Series 中的两个元素之间粘贴,仅使用 Pandas / Numpy。注意:文中所有带有href的行都不一样。

import pandas as pd
import numpy as np

table = pd.Series(

        ["<td class='test'>AA</td>",                  # 0 
        "<td class='test'>A</td>",                    # 1
        "<td class='test'><a class='test' href=...",  # 2
        "<td class='test'>B</td>",                    # 3
        "<td class='test'><a class='test' href=...",  # 4
        "<td class='test'>BB</td>",                   # 5
        "<td class='test'>C</td>",                    # 6
        "<td class='test'><a class='test' href=...",  # 7 
        "<td class='test'>F</td>",                    # 8
        "<td class='test'>G</td>",                    # 9 
        "<td class='test'><a class='test' href=...",  # 10 
        "<td class='test'>X</td>"])                   # 11


dups = ~table.str.contains('href') & table.shift(-1).str.contains('href') 
array = np.insert(table.values, dups[dups].index, "None")
pd.Series(array)


# OUTPUT:
# 0                      <td class='test'>AA</td>
# 1                                          None
# 2                       <td class='test'>A</td>
# 3     <td class='test'><a class='test' href=...
# 4                                          None Incorrect
# 5                       <td class='test'>B</td>
# 6     <td class='test'><a class='test' href=...
# 7                      <td class='test'>BB</td>
# 8                                          None
# 9                       <td class='test'>C</td>
# 10    <td class='test'><a class='test' href=...
# 11                      <td class='test'>F</td>
# 12                                         None
# 13                      <td class='test'>G</td>
# 14    <td class='test'><a class='test' href=...
# 15                      <td class='test'>X</td>

这是我想要的实际文本输出。

# OUTPUT:
# 0                      <td class='test'>AA</td>
# 1                                          None
# 2                       <td class='test'>A</td>
# 3     <td class='test'><a class='test' href=...
# 4                       <td class='test'>B</td>
# 5     <td class='test'><a class='test' href=...
# 6                      <td class='test'>BB</td>
# 7                                          None
# 8                       <td class='test'>C</td>
# 9     <td class='test'><a class='test' href=...
# 10                      <td class='test'>F</td>
# 11                                         None
# 12                      <td class='test'>G</td>
# 13    <td class='test'><a class='test' href=...
# 14                      <td class='test'>X</td>

【问题讨论】:

    标签: python pandas numpy text iteration


    【解决方案1】:

    您可以执行与以前相同的程序。

    唯一需要注意的是,您必须在班次之前执行非 (~) 运算符。原因是移位将在您的系列的第一个位置创建一个 np.nan,这会将系列定义为浮点数,因此在 not 操作上失败。

    import pandas as pd
    import numpy as np
    
    table = pd.Series(
            ["<td class='test'>AA</td>",                  # 0 
            "<td class='test'>A</td>",                    # 1
            "<td class='test'><a class='test' href=...",  # 2
            "<td class='test'>B</td>",                    # 3
            "<td class='test'><a class='test' href=...",  # 4
            "<td class='test'>BB</td>",                   # 5
            "<td class='test'>C</td>",                    # 6
            "<td class='test'><a class='test' href=...",  # 7 
            "<td class='test'>F</td>",                    # 8
            "<td class='test'>G</td>",                    # 9 
            "<td class='test'><a class='test' href=...",  # 10 
            "<td class='test'>X</td>"])                   # 11
    
    
    not_contain = ~table.str.contains('href')
    cond = not_contain & not_contain.shift(1)
    array = np.insert(table.values, cond[cond].index, "None")
    pd.Series(array)
    

    【讨论】:

    • 再次干得好@ecortazar!
    【解决方案2】:

    这解决了上述问题,但没有 Numpy 和 Pandas。如果你能和他们一起重建,我会给你正确的答案。

    import pandas as pd
    import numpy as np
    
    table = pd.Series(
    
            ["<td class='test'>AA</td>",                  # 0 
            "<td class='test'>A</td>",                    # 1
            "<td class='test'><a class='test' href=...",  # 2
            "<td class='test'>B</td>",                    # 3
            "<td class='test'><a class='test' href=...",  # 4
            "<td class='test'>BB</td>",                   # 5
            "<td class='test'>C</td>",                    # 6
            "<td class='test'><a class='test' href=...",  # 7 
            "<td class='test'>F</td>",                    # 8
            "<td class='test'>G</td>",                    # 9 
            "<td class='test'><a class='test' href=...",  # 10 
            "<td class='test'>X</td>"])                   # 11
    
    
    insertAt = []
    for i in range(0, len(table)-1):
      # print('count ', i)
    
      if i == 1:
        if 'href' not in table[0] and 'href' not in table[1]:
          print(i, ' starts with tag')
          print(i, ' is duplicated')
          insertAt.append(True) 
          insertAt.append(True) 
          next
        elif 'href' not in table[0] and 'href' in table[1]:
          print(i, ' not start with tag')
          print(i, ' is not duplicated')
          insertAt.append(True) 
          insertAt.append(False) 
          next
    
        else:
          print(i, ' not start with tag')
          print(i, ' is not duplicated')
          insertAt.append(False) 
          insertAt.append(False) 
          next
    
      if i > 1:
    
        if 'href' not in table[i-1] and 'href' not in table[i]: 
          print(i + 1, ' is duplicated')
          insertAt.append(True)
    
        else:
          print(i + 1, ' is not duplicated')
          insertAt.append(False)
    
    insertAt = pd.Series(insertAt)
    array = np.insert(table.values, insertAt[insertAt].index, "None")
    pd.Series(array) # back to series if necessary
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-02
      • 2015-01-29
      • 1970-01-01
      • 2020-10-31
      相关资源
      最近更新 更多