大熊猫:在分隔符的基础上过滤包含特定列的数据的行

时间:2019-06-20 07:39:06

标签: python pandas dataframe split

当列 director 中的任何值带有分隔符“ |”时,我试图仅返回那些行。但是它不是基于分隔符进行过滤,而是显示所有行。请让我知道与此有关的可能问题。

我尝试了以下操作:

hb_dctr = df_updated[df_updated['director'].str.contains('|')]
hb_dctr

但它显示以下内容

id      popularity   budget     Cast                        director
135397  32.985763    150000000  Chris Pratt|Irrfan Khan     Colin Trevorrow
76341   28.419936    150000000  Tom Hardy|Charlize Theron   George Miller
76757   6.189369     176000003  Mila Kunis|Channing     Lana Wachowski|Lilly Wachowski

它应该只显示ID为 135397 766341

的行

2 个答案:

答案 0 :(得分:2)

您需要设置var myObject = [ { "ProId": 12, "ProName": "Samsung Galaxy A9", "AttriValue": { "ProductId": "12", "Front Camera": "16 MP and Above", "Internal Memory": "128 GB and Above", "Network Type": "4G", "Primary Camera": "16 MP and Above", "Ram": "6 GB" } }, { "ProId": 11, "ProName": "Vivo Y95", "AttriValue": { "ProductId": "11", "Front Camera": "16 MP and Above", "Internal Memory": "64 GB", "Network Type": "4G", "Primary Camera": "13 - 15.9 MP", "Ram": "4 GB" } }, { "ProId": 10, "ProName": "OPPO A7", "AttriValue": { "ProductId": "10", "Front Camera": "16 MP and Above", "Internal Memory": "64 GB", "Network Type": "4G", "Primary Camera": "13 - 15.9 MP", "Ram": "4 GB" } } { "ProId": 16, "ProName": "Samsung Feature Phone", "AttriValue": { "Sim Type": "Single", } } ]

var myFilter = [
  ["Front Camera", "16 MP and Above"],
  ["Front Camera", "8 - 11.9 MP"], ​
  ["Internal Memory", "128 GB and Above"], ​
  ["Primary Camera", "16 MP and Above"],
  ["Primary Camera", "8 - 12.9 MP"],
  ["Network Type", "4G"],
  ["Primary Camera", "16 MP and Above"], ​
  ["Ram", "4 GB"],
  ["Sim Type", "Single"]
] 

regex=False

如果要排除此类行,请使用反df[df.director.str.contains("|",regex=False)]

      id  popularity     budget                 Cast  \
2  76757    6.189369  176000003  Mila Kunis|Channing   

                         director  
2  Lana Wachowski|Lilly Wachowski  

~

答案 1 :(得分:1)

转义extension YourViewController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.view.endEditing(true) } } ,因为特殊的正则表达式字符(|):

or

对于不包含使用df1 = df[df.director.str.contains("\|")] print (df1) id popularity budget Cast \ 2 76757 6.189369 176000003 Mila Kunis|Channing Lana director 2 Wachowski|Lilly Wachowski

~

详细信息

df2 = df[~df.director.str.contains("\|")]
print (df2)
       id  popularity     budget                       Cast         director
0  135397   32.985763  150000000    Chris Pratt|Irrfan Khan  Colin Trevorrow
1   76341   28.419936  150000000  Tom Hardy|Charlize Theron    George Miller