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

Condition within for loop is ambiguous

I am trying to run a conditional for loop on a dataframe. If the condition is not met, then the last row is appended to the result dataframe.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(100, size=(100,3)), columns=['Value1', 'Value2', 'Value3'])

split_df = [df[i:i+12] for i in range(0, len(df))]

result = []
for i in split_df:
    if i == 0:
        result = split_df[i]
    else:
        last_row = split_df[i].tail(1)
        result = result.append(last_row, ignore_index=True)

For If i == 0:, I get the following error:

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

How do I get around this?

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 :

i is a DataFrame, because you’re looping over a list of DataFrames. You probably want to write the loop like this:

for i, df in enumerate(split_df):
    if i == 0:
        result = df
    else:
        last_row = df.tail(1)
        result.append(last_row, ignore_index=True)

Also note that you don’t want to write result = result.append(...) – rather, you’d just write result.append(...) because append() modifies the loop in-place, and it doesn’t return anything.

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