Check if values in each first coulmn in df startswith ! using pandas

types={}
for col in df.columns[0]:
    if df[col].dtype == object:
        print('Check values inside column')
              if '!' in df[col].values :
              print("\nThis value exists in Dataframe")

I have a couple of the data frames. I need to check two thigs:

  • if the first column in each df is string, if so, then
  • if any value inside that column starts with !. I am trying to playing around with this code, but I’m getting a key error.

>Solution :

This should do the trick:

df.iloc[:, 0].astype(str).str.startswith('!').any()

This assumes that the other possible dtypes do not result in a string representation that starts with a !, which should work in the vast majority of applications.

Leave a Reply