0

I wanted to create a new dataframe on pandas based on conditions of the current dataframe. If the current dataframe is > 1000 rows then make the data frame empty (delete rows). Otherwise then just use the current dataframe. Below is my code that is having an error:

DF_NEW = np.where(len(DF)>1000, pd.DataFrame(DF[0:0]), pd.DataFrame(DF))

3 Answers 3

1

You should use:

DF_NEW = pd.DataFrame(columns=DF.columns) if len(DF) > 1000 else DF.copy()

np.where need a vector with the same shape of your condition vector.

Sign up to request clarification or add additional context in comments.

Comments

1

Use if else as:

if len(DF)>1000:
    DF_NEW = pd.DataFrame(columns=DF.columns)
else:
    DF_new = DF.copy()

Comments

0
ìf len(df) > 1000:
    for label in df.columns: 
        df.pop('label')
elif len(df) <= 1000:
    pass

your current code will make an array where, if the condition is True, it will take the first, and if condition is False, the latter

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.