Skip to content

Instantly share code, notes, and snippets.

@stefanthoss
Last active November 12, 2019 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanthoss/5f2f82b7b7173e5336e53856ceb2004c to your computer and use it in GitHub Desktop.
Save stefanthoss/5f2f82b7b7173e5336e53856ceb2004c to your computer and use it in GitHub Desktop.
List all columns of a DataFrame that contain a certain character at least once.
import pandas as pd
# Input:
#            a     b        c    d    e
# 0  Text       Text  NaN      0.0  5
# 1  Text       NaN   1.1.1.1  0.0  55
# 2  Text.Text  Text  Text     0.4  555
data = [
    {"a": "Text", "b": "Text", "d": 0, "e": 5},
    {"a": "Text", "c": "1.1.1.1", "d": 0, "e": 55},
    {"a": "Text.Text", "b": "Text", "c": "Text", "d": 0.4, "e": 555},
]
df = pd.DataFrame(data)
[c for c in df.columns if (df[c].astype(str).str.find(".") > -1).any()]
# Output:
# ['a', 'd', 'c']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment