python - numpy 中的多个插入,其中配对元素没有潜台词

标签 python pandas numpy text iteration

此问题是 this previous post 的后续问题由@ecortazar 回答。但是,我还想仅使用 Pandas/Numpy 在 pd.Series 中不包含特定字符串的两个元素之间粘贴。注意:文本中所有带有 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>

最佳答案

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

唯一需要注意的是,您必须在转换之前执行非 (~) 运算符。原因是移位将在 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)

关于python - numpy 中的多个插入,其中配对元素没有潜台词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54953254/

相关文章:

python - 将带有字符串的结构化 numpy 数组传递给 cython 函数

python - 值错误 : object too deep for desired array

python - GeoAlchemy2:从几何列中提取地理属性

python - 组合 DataFrame 时保留索引和列顺序

python - 如何让 PyC​​harm 在安装库时忽略 SSL 错误?

python - 当存在 locals() 时,是否有可以忽略 ' assigned to but never used' 的 python 语法检查器?

python - 将多行连接成一行 Pandas

python - 使用另一个数据框的值更新数据框 - python

python - 如何将 numpy.array2string 形成的字符串转换回数组?

python - 在 pandas 数据框中进行分组并合并 numpy 数组列