Insert multiple elements into Pandas Series where similarities exist

回眸只為那壹抹淺笑 提交于 2019-12-25 01:06:26

问题


Here I'd like to insert the row "<td class='test'>None</td>" between wherever there are two rows with "href" in the tag--note, each row with href is NOT identical.

import pandas as pd

table = pd.Series(

        ["<td class='test'><a class='test' href=...", # 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'><a class='test' href=...",  # 5
        "<td class='test'>C</td>",                    # 6
        "<td class='test'><a class='test' href=...",  # 7 
        "<td class='test'>F</td>",                    # 8
        "<td class='test'><a class='test' href=...",  # 9 
        "<td class='test'><a class='test' href=...",  # 10 
        "<td class='test'>X</td>"])                   # 11

insertAt = []
for i in range(0, len(table)):
  if 'href' in table[i] and 'href' in table[i+1]:
    print(i + 1, ' is duplicated')
    insertAt.append(i)

# 5  is duplicated
# 10  is duplicated
# [4, 9]

Here's what the output should look:

#         ["<td class='test'><a class='test' href=...", # 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'>None</td>",                 # 5 Insert "<td class='test'>None</td>"
#         "<td class='test'><a class='test' href=...",  # 6
#         "<td class='test'>C</td>",                    # 7
#         "<td class='test'><a class='test' href=...",  # 8 
#         "<td class='test'>F</td>",                    # 9
#         "<td class='test'><a class='test' href=...",  # 10
#         "<td class='test'>None</td>",                 # 11 Insert <td class='test'>None</td>"
#         "<td class='test'><a class='test' href=...",  # 12 
#         "<td class='test'>X</td>"]                    # 13

回答1:


Than can be easily achieved if you go to numpy.

In your example:

dups = table.str.contains('href') & table.shift(1).str.contains('href')

array = np.insert(table.values, dups[dups].index, "<td class='test'>None</td>")

pd.Series(array)



回答2:


Ecotrazar's solution above is both faster and more elegant. Here is my version using for loops and his numpy insert method.

import pandas as pd

table = pd.Series(

        ["<td class='test'><a class='test' href=...", # 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'><a class='test' href=...",  # 5
        "<td class='test'>C</td>",                    # 6
        "<td class='test'><a class='test' href=...",  # 7 
        "<td class='test'>F</td>",                    # 8
        "<td class='test'><a class='test' href=...",  # 9 
        "<td class='test'><a class='test' href=...",  # 10 
        "<td class='test'>X</td>"])                   # 11

insertAt = []
for i in range(0, len(table)):

  if 'href' in table[i] and 'href' in table[i + 1] and i == 0:
    print(i + 1, ' is duplicated')
    insertAt.append(True)
  elif i == 0:
     insertAt.append(False)

  if 'href' in table[i] and 'href' in table[i+1] and i > 0:
    print(i + 1, ' is duplicated')
    insertAt.append(True)

  else:
    insertAt.append(False)

insertAt = pd.Series(insertAt)
print(insertAt)

import numpy as np
array = np.insert(table.values, insertAt[insertAt].index, "<td class='test'>None</td>")
pd.Series(array) # back to series if necessary


来源:https://stackoverflow.com/questions/54949167/insert-multiple-elements-into-pandas-series-where-similarities-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!