在这里,我想在标签中插入两个行带有" href"的任何地方之间的行 "<td class='test'>None</td>"
-注意,带有href的每行都不相同。
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]
这是输出应该看起来的:
# ["<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
如果您转到numpy,可以轻松实现。
在您的示例中:
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)
ecotrazar上面的解决方案既更快又优雅。这是我的版本用于循环和他的numpy插入方法。
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