0

I have many columns name with string with (t+1) (t+2) .... (t+54) In pictured attached, How can I drop just only columns "lemon(t+1),....,lemon(t+30), not lemon(t-1)

Thanks

enter image description here

2 Answers 2

1

See comments in code

cols = df.columns  # get columns of the dataframe
# filter the list of columns by removeing items from 'lemon(t+1)' to 'lemon(t+30)'
cols2 = filter(lambda x: not (x.startswith('lemon(t+') and x[8:-1]>0 and x[8:-1]<31) )
df_result = df[cols2]  # create needed dataframe
Sign up to request clarification or add additional context in comments.

Comments

1

using a list comprehension -

df.drop([f"lemon(t-{i})" for i in range(1,31)],axis=1)

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.