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

How to find substring in dataframe in python

I would like to find a substring "sam" in the dataframe "df1". For example:

df1 = pd.DataFrame({'a': [1, 'sam right'], 'b': [7, 8]})
df2 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
print(df1)
print(df2)
result = df1.str.contains("sam")
print("Result",result)
if result:
  print('*********Element exists in Dataframe************')
else:
  print('*********Element does not exists in Dataframe************')

output gives error as below. Please help.

 return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'str'. Did you mean: 'std'?

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

>Solution :

Your line

result = df1.str.contains("sam")

Is close to be fine. First you can convert the DataFrame to a string:

df1.__str__()

And then check if "sam" is in the string

result = "sam" in df1.__str__()
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