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')
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.