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')
JD2775
  • 3,658
  • 7
  • 30
  • 52
  • [Series Accessors](https://pandas.pydata.org/pandas-docs/stable/reference/series.html#accessors) – wwii Jan 25 '20 at 02:08
  • Explicit loops should be a last resort, read the Pandas docs. – AMC Jan 25 '20 at 03:40
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – AMC Jan 25 '20 at 03:40

1 Answers1

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')
Allen Qin
  • 19,507
  • 8
  • 51
  • 67