Browse Source

added pandas filter-column-by-regex example

George Jones 1 year ago
parent
commit
ba29a2f525
1 changed files with 28 additions and 0 deletions
  1. 28 0
      home/public/snippits/pandas/filter_df_by_regex.py

+ 28 - 0
home/public/snippits/pandas/filter_df_by_regex.py

@@ -0,0 +1,28 @@
+#! /usr/bin/env python3
+# filter dataframe by matching regex
+
+# https://stackoverflow.com/questions/37080612/pandas-dataframe-filter-regex/37080814#37080814
+
+import pandas as pd
+data = {
+    'Company' : ['Ford','Ford','Ford','Ford','Chevy','Chevy'],
+    'Type' : ['Mercury','Lincoln','Lincoln','Econoline','Malabu','Pickups'],
+    'Profit' : [1,100,40,99,2,3]
+}
+df = pd.DataFrame(data)
+
+# print(df)
+
+#   Company       Type  Profit
+# 0    Ford    Mercury       1
+# 1    Ford    Lincoln     100
+# 2    Ford    Lincoln      40
+# 3    Ford  Econoline      99
+# 4   Chevy     Malabu       2
+# 5   Chevy    Pickups       3
+
+# Now print only rows that have an "e" in the Type
+# preceeded by a capitol letter (that's how I choose
+# my cars :-))
+
+print(df[df["Type"].str.contains('[A-Z]e',regex=True)])