Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading