假设我有一个像这样的 pandas dframe :
Name Nos
Jack [4,2,3,12]
Adam [5,6,4,2,3]
John [1,3,4]
最快的方法是什么(简短代码),当输入为 [4,2,3]
时,结果应该是包含行 Jack 和 Adam 的 dframe ?我尝试过使用 where,但我不太能得到正确的解决方案。
最佳答案
如果您需要让值[4, 2, 3]
连续出现在列表中,您可以使用以下代码:
input_list = [4, 2, 3]
def func(df, l):
return df[df['Nos'].astype(str).str.contains(str(l)[1:-1])]
print(func(df, input_list))
如果您只想将值放在那里,请使用:
input_list = [4, 2, 3]
def func(df, l):
return df[df['Nos'].astype(str).str.contains('&'.join(l))]
print(func(df, input_list))
关于python - 选择 pandas dframe 中的行,其中给定的输入列表是 dframe 列中列表的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59871960/