-2

I need an expression which checks if there is any content written inside braces ("braces" could be any of (), {}, []). For Example it should give True to these strings:

  • "I am sharad my phone number is (0000000)"
  • "hi this [sharad]"
  • "{there} you go"
  • "[this is error]"

And should give False to:

  • "ho ho ho"
  • "here comes santa []"
  • "{} what is this"

If there is any content inside braces, return True, else False. I am new to Python thanks for your help in advance.

For example:

df = pd.read_excel(file)
df.index = range(2,df.shape[0]+2)
for index,row in df.iterrows():
    if(row['column_name']******) # expression to check if there is any 
                                 # content inside brackets content could be 
                                 # nan, string or a number                          
Alex Myers
  • 6,196
  • 7
  • 23
  • 39
SHARAD SAURAV
  • 35
  • 1
  • 7
  • 2
    hi. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Sep 25 '19 at 12:30
  • 1
    Thank you I did and it surely helped – SHARAD SAURAV Sep 25 '19 at 13:27
  • Yes, the best is remove question and create another one, also add new tag `regex` – jezrael Sep 25 '19 at 13:28

1 Answers1

0

Starting with this data:

import pandas as pd

df = pd.DataFrame({
    'col' : [
        "I am sharad my phone number is (0000000)",
        "hi this [sharad]",
        "{there} you go",
        "[this is error]",
        "ho ho ho",
        "here comes santa []",
        "{} what is this",
    ]
})

print(df)

                                        col
0  I am sharad my phone number is (0000000)
1                          hi this [sharad]
2                            {there} you go
3                           [this is error]
4                                  ho ho ho
5                       here comes santa []
6                           {} what is this

We can check for the "brackets" and then check if they contain anything using regex and the contains() method:

print(df.col.str.contains('\[(.+)\]|\((.+)\)|\{(.+)\}'))

0     True
1     True
2     True
3     True
4    False
5    False
6    False
Name: col, dtype: bool

Documentation for contains

Explanation for regex:

  • for the [] I used \[ and \] to look for those characters. Then inside of them I used (.+) which checks if there is anything between the two brackets.

I did the same using the other two versions of "brackets" (() and {})

The | is an or operator so the contains knows to check for any of the brackets.

Other resources

Ian Thompson
  • 2,914
  • 2
  • 18
  • 31