0

I have a dataframe with 5 columns, one of which is 'TABLE_NAME'. That column has values such as:

A_value1
B_value1
B_value2
A_value150

I want to print those that start with 'A_' only.

I tried this but its returning the following:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Here is the code:

value = 'A_'
for index, row in df.iterrows():
    if df.loc[df['TABLE_NAME'].str.contains(value)]:
        print('y')
    else:
        print('n')
3

1 Answer 1

1

You can use str.startswith:

df.loc[df.TABLE_NAME.str.startswith('A_')]

If you want to use your for loop, you can do:

value = 'A_'
for index, row in df.iterrows():
    if row['TABLE_NAME'].startswith(value):
        print('y')
    else:
        print('n')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.