Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete pandas column if column name begins with a number

Tags:

python

pandas

I have a pandas DataFrame with about 200 columns. Roughly, I want to do this

for col in df.columns:
    if col begins with a number:
        df.drop(col)

I'm not sure what are the best practices when it comes to handling pandas DataFrames, how should I handle this? Will my pseudocode work, or is it not recommended to modify a pandas dataframe in a for loop?

like image 355
Gen Tan Avatar asked May 04 '18 06:05

Gen Tan


2 Answers

I think simpliest is select all columns which not starts with number by filter with regex - ^ is for start of string and \D is for not number:

df1 = df.filter(regex='^\D')

Similar alternative:

df1 = df.loc[:, df.columns.str.contains('^\D')]

Or inverse condition and select numbers:

df1 = df.loc[:, ~df.columns.str.contains('^\d')]

df1 = df.loc[:, ~df.columns.str[0].str.isnumeric()]

If want use your pseudocode:

for col in df.columns:
    if col[0].isnumeric():
        df = df.drop(col, axis=1)

Sample:

df = pd.DataFrame({'2A':list('abcdef'),
                   '1B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D3':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'F':list('aaabbb')})

print (df)
   1B 2A  C  D3  E  F
0   4  a  7   1  5  a
1   5  b  8   3  3  a
2   4  c  9   5  6  a
3   5  d  4   7  9  b
4   5  e  2   1  2  b
5   4  f  3   0  4  b

df1 = df.filter(regex='^\D')
print (df1)
   C  D3  E  F
0  7   1  5  a
1  8   3  3  a
2  9   5  6  a
3  4   7  9  b
4  2   1  2  b
5  3   0  4  b
like image 104
jezrael Avatar answered Oct 01 '22 16:10

jezrael


An alternative can be this:

columns = [x for x in df.columns if not x[0].isdigit()]
df = df[columns]
like image 34
Joe Avatar answered Oct 01 '22 16:10

Joe