【问题标题】:How to print row that contain list with specific values python如何打印包含具有特定值python的列表的行
【发布时间】:2021-07-18 08:32:52
【问题描述】:

我想打印包含 'c' 作为作者的 df。这是我的数据框:

author title
['a','b','c'] x
andin y
NaN z

如果我以作者身份搜索“c”,输出如下:

author title
['a','b','c'] x

我的代码是这样的:

df.apply(lambda x: any(['c' in x['author']]), axis=1)

但输出会像'TypeError:'float'类型的参数不可迭代'

【问题讨论】:

  • ['a','b','c'] 是实际列表还是看起来像列表的字符串?
  • @Timus 它是一个列表

标签: python list dataframe


【解决方案1】:

删除NaN 值后,您需要在loc 内的df['author'] 列上使用apply

df1 = df.dropna()
print(df1.loc[df1['author'].apply(lambda x: 'andin' in x)])

【讨论】:

  • @A.NurulIstiqamah 编辑了答案
猜你喜欢
  • 1970-01-01
  • 2016-02-12
  • 2015-07-06
  • 2022-12-12
  • 2013-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多