python pandas: case insensitive drop column -
i have df , want drop column label in case insensitive way. note: don't want change in df i'd avoid 'str.lower'.
heres df:
print df name unweightedbase base q6a1 q6a2 q6a3 q6a4 q6a5 q6a6 esubtotal name base 1006 1006 100,00% 96,81% 96,81% 96,81% 96,81% 3,19% 490,44% q6_6 31 32 100,00% - - - - - - q6_3 1006 1006 43,44% 26,08% 13,73% 9,22% 4,34% 3,19% 100,00% q6_4 1006 1006 31,78% 31,71% 20,09% 10,37% 2,87% 3,19% 100,00%
is there magic can apply code below?
df.drop(['unweightedbase', 'q6a1'],1)
i think can create function perform case-insensitive search you:
in [90]: # create noddy df df = pd.dataframe({'unweightedbase':np.arange(5)}) print(df.columns) # create list of column names col_list = list(df) # define our function perform case-insensitive search def find_col_name(name): try: # uses generator find index if matches, raise exception if not found return col_list[next(i i,v in enumerate(col_list) if v.lower() == name)] except: return '' df.drop(find_col_name('unweightedbase'),1) index(['unweightedbase'], dtype='object') out[90]: empty dataframe columns: [] index: [0, 1, 2, 3, 4]
my search code attributed one: find index of string ignoring cases
Comments
Post a Comment