numpy中的多个插入,其中成对的元素没有子文本

时间:2019-03-01 22:36:35

标签: python pandas numpy text iteration

this previous post之后,这个问题得到了@ecortazar的回答。但是,我也想在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>

2 个答案:

答案 0 :(得分:1)

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

唯一的警告是您必须在转换前执行not(〜)运算符。原因是该移位将在Series的第一个位置创建一个np.nan,这会将Series定义为float,从而导致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)

答案 1 :(得分:0)

这解决了上述问题,但没有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