在熊猫列中搜索多字符串

时间:2021-06-22 09:35:18

标签: python pandas string numpy

我有一个如下所示的 Pandas 数据框:

       col1   col2
   0   1      A
   1   10     A, B
   2   20     B
   3   5      C
   4   70     A, B, C

现在我想在给定条件下搜索 col2 并相应地选择行。例如:

search_pattern = ["A"] -> Select all rows where A is present [rows 0, 1, 4]
search_pattern = ["A", "B"] -> Select all rows where A is present and B is present [rows 1, 4]
search_pattern = ["B"] -> Select all rows where B is present [rows 1, 2, 4]

    

2 个答案:

答案 0 :(得分:1)

您可以通过Series.map中的<class '__main__.PointInTime'>: comparing 2015-1-1 to 2015-2-1 -> 2015-2-5 (Timespan in PointInTime) gives False <class '__main__.PointInTime'>: comparing 2015-1-1 to 2015-2-1 -> 2015-4-1 (Timespan in PointInTime) gives False <class '__main__.PointInTime'>: comparing 2015-1-1 to 2015-2-1 -> 2015-2-5 (Timespan in PointInTime) gives False <class '__main__.PointInTime'>: comparing 2015-2-2 to 2015-2-1 -> 2015-2-5 (Timespan in PointInTime) gives True <class '__main__.PointInTime'>: comparing 2015-2-2 to 2015-2-1 -> 2015-4-1 (Timespan in PointInTime) gives True <class '__main__.PointInTime'>: comparing 2015-2-2 to 2015-2-1 -> 2015-2-5 (Timespan in PointInTime) gives True <class '__main__.PointInTime'>: comparing 2015-3-3 to 2015-2-1 -> 2015-2-5 (Timespan in PointInTime) gives False <class '__main__.PointInTime'>: comparing 2015-3-3 to 2015-2-1 -> 2015-4-1 (Timespan in PointInTime) gives True <class '__main__.PointInTime'>: comparing 2015-3-3 to 2015-2-1 -> 2015-2-5 (Timespan in PointInTime) gives False <class '__main__.PointInTime'>: comparing 2015-4-4 to 2015-2-1 -> 2015-2-5 (Timespan in PointInTime) gives False <class '__main__.PointInTime'>: comparing 2015-4-4 to 2015-2-1 -> 2015-4-1 (Timespan in PointInTime) gives False <class '__main__.PointInTime'>: comparing 2015-4-4 to 2015-2-1 -> 2015-2-5 (Timespan in PointInTime) gives False pit vals1 0 2015-1-1 1 1 2015-2-2 2 2 2015-3-3 3 3 2015-4-4 4 ts vals2 0 2015-2-1 -> 2015-2-5 a 1 2015-2-1 -> 2015-4-1 b 2 2015-2-1 -> 2015-2-5 c pit vals1 ts vals2 0 2015-2-2 2 2015-2-1 -> 2015-2-5 a 1 2015-2-2 2 2015-2-1 -> 2015-4-1 b 2 2015-2-2 2 2015-2-1 -> 2015-2-5 c 3 2015-3-3 3 2015-2-1 -> 2015-4-1 b 拆分值并与集合进行比较:

location / {
    proxy_pass YOUR_SERVER;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
}

issubset

答案 1 :(得分:0)

如果您的 DataFrame 存储在变量 df 中,您可以这样做

df[df.col2.apply(
  lambda c: all(el in c.replace(' ', '').split(',') for el in search_pattern)
)]