熊猫数据框从数组中选择值

时间:2019-03-19 20:24:58

标签: arrays pandas select

我有一个熊猫数据框,如下图所示。我想选择所有在路由字段中包含特定值的数据框行(4008182)。我已经尝试过类似df ['routes']。isin([4008182])的方法,但是它不起作用。有什么建议么?谢谢
enter image description here

1 个答案:

答案 0 :(得分:0)

使用Pandas.Series.str.contains(),此代码应能解决问题:

df[df.routes.str.contains('4008182')]

编辑:

考虑路由列是数字列表,下面的解决方案使用apply / lambda进行了补充:

df[df.apply(lambda x: 4008182 in x['routes'], axis=1)]