Skip to content
Advertisement

Extract only cells containing a string from pandas table and copy them into a new table

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
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement