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?
CodePudding user response:
i
is a DataFrame
, because you're looping over a list of DataFrame
s. 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.