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?
JavaScript
x
16
16
1
import pandas as pd
2
3
df1 = pd.DataFrame([
4
{'a': 'sky is blue', 'b': 7},
5
{'a': 'fire is red', 'b': 9},
6
{'a': 'water is blue', 'b': 8},
7
])
8
9
df2 = df1.loc[df1.a.str.contains('blue'), :]
10
11
# df2 is now:
12
#
13
# a b
14
# 0 sky is blue 7
15
# 2 water is blue 8
16