考虑这个 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/