Python 如何使用字符串和整数的组合删除列?

Python 如何使用字符串和整数的组合删除列?,python,pandas,dataframe,Python,Pandas,Dataframe,我有许多列的名称和字符串(t+1)(t+2)。。。。(t+54) 在所附的图片中,如何仅删除列“lemon(t+1)”、…、lemon(t+30)而不是lemon(t-1) 谢谢 使用列表理解- df.drop([f"lemon(t-{i})" for i in range(1,31)],axis=1) 使用列表理解- df.drop([f"lemon(t-{i})" for i in range(1,31)],axis=1) 请参见代码中的注释 cols = df.columns # g

我有许多列的名称和字符串(t+1)(t+2)。。。。(t+54) 在所附的图片中,如何仅删除列“lemon(t+1)”、…、lemon(t+30)而不是lemon(t-1)

谢谢


使用列表理解-

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

使用列表理解-

df.drop([f"lemon(t-{i})" for i in range(1,31)],axis=1)
请参见代码中的注释

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
cols=df.columns#获取数据帧的列
#通过将项目从“lemon(t+1)”移除到“lemon(t+30)”来筛选列列表
cols2=filter(λx:not(x.startswith('lemon(t+))和x[8:-1]>0和x[8:-1]参见代码中的注释

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
cols=df.columns#获取数据帧的列
#通过将项目从“lemon(t+1)”移除到“lemon(t+30)”来筛选列列表
cols2=过滤器(λx:not(x.startswith('lemon(t+))和x[8:-1]>0和x[8:-1]