python - 给定一个 pandas 数据框,如何检查和计算行字符串是否位于一行嵌套列表内?

标签 python python-3.x pandas

考虑这个 pandas df:

在:

import pandas as pd
pd.set_option('display.max_colwidth', -1)

df = pd.DataFrame({'col1':['the quick brown fox','hi hello','hello there','good morning'], 
                   'col2': [[['the quick brown fox'],['this is a test'], ['how is it going']],
                            ['lore lipsum dolor'],
                            [''],[['good'],['morning']]]})
df

输出:

    col1                      col2
0   the quick brown fox     [[the quick brown fox], [this is a test], [how is it going]]
1   hi hello                [lore lipsum dolor]
2   hello there                  []
3   good morning            [[good], [morning]]

如何检查 col1 的完整字符串是否位于 col2 嵌套列表内的任何列表内?例如,预期输出应该看起来像这样的新列:

在:

df = pd.DataFrame({'col3':[[1,0,0,0],[0],[0], [[0],[0]]]})
df

输出:

      col3
0   [1, 0, 0]
1   [0]
2   [0]
3   [0, 0]

例如尝试并希望将这个想法应用到我的 df 列中:

s = 'the quick brown fox'
l = [['the quick brown fox'],['this is a test'], ['how is it going']]

    a_lis = []
    for e in l:
        if s in e:
            a_lis.append(1)
        else:
            a_lis.append(0)

    print(a_lis)

最佳答案

使用 in 的嵌套列表理解并将 bool 值转换为整数:

df['col3'] = [[int(a in x) for x in b] for a, b in zip(df['col1'], df['col2'])]

print (df)
                  col1                                               col2  \
0  the quick brown fox  [[the quick brown fox], [this is a test], [how...   
1             hi hello                                [lore lipsum dolor]   
2          hello there                                                 []   
3         good morning                                [[good], [morning]]   

        col3  
0  [1, 0, 0]  
1        [0]  
2        [0]  
3     [0, 0]  

关于python - 给定一个 pandas 数据框,如何检查和计算行字符串是否位于一行嵌套列表内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56311222/

相关文章:

python - 使用Python访问JSON文件,得到 "Memory Error"

python - 如何从python中的文本中获取字体大小

python - Groupby并从另一列获取值

python - 从数据框中选择随机值,以便生成的数据框在 python-pandas 的两列中是唯一的

python - 子进程 popen 标准输出

python - 如何将 xls 中的所有字段作为字符串导入 Pandas 数据框?

python - 包含分类数据和数值数据的 Pandas 条形图

python - 如何在Redis中创建一个具有多个节点的Elasticache?

python - 如何在 Outlook (2010) 全局地址列表中搜索多个姓名?

python-3.x - 在 Bitbucket 管道中为 python 代码编写测试用例