V_s*_*qrt 1 python string pandas
我有一个多列以数字开头的数据框。例如,
00am 00pm 02am 03am 10am 10k 11th 12th 13th ... ABC JOIN
example example example ... ... ... ... ... example example
Run Code Online (Sandbox Code Playgroud)
我想删除所有以数字开头的列
... ABC JOIN
... example example
Run Code Online (Sandbox Code Playgroud)
我用过str和isdigit
df.loc[:,~df.columns.str.isdigit()]
Run Code Online (Sandbox Code Playgroud)
但结果仍然显示以数字开头的列。同样,我应该排除包含数字的列(我会说在这种情况下我应该使用str.contain())。
使用正则表达式检查列名
df = pd.DataFrame([[1,2,3,4,5]],columns=['0a','0b','0c','0d','e'])
0a 0b 0c 0d e
0 1 2 3 4 5
df[df.columns[~df.columns.str.match('^\d')]]
e
0 5
Run Code Online (Sandbox Code Playgroud)