I have a huge pandas table, with many rows and columns. I want to pull all the cells that contain a specific string and create a new table containing only those. Any ideas on how to approach this?
Thank you!
Advertisement
Answer
Do you mean something like this?
import pandas as pd
df1 = pd.DataFrame([
{'a': 'sky is blue', 'b': 7},
{'a': 'fire is red', 'b': 9},
{'a': 'water is blue', 'b': 8},
])
df2 = df1.loc[df1.a.str.contains('blue'), :]
# df2 is now:
#
# a b
# 0 sky is blue 7
# 2 water is blue 8