【发布时间】:2020-09-23 20:54:51
【问题描述】:
我有以下输出
Age
'1 year old',
'14 years old',
'music store',
'7 years old ',
'16 years old ',
使用这行代码后创建
df['Age']=df['Age'].str.split('.', expand=True,n=0)[0]
df['Age'].tolist()
我想从数据集中删除不以数字或数字+年+旧或数字+年+开头的行(最好使用它的副本或过滤后的新行)老的。
预期输出
Age (in a new dataset filtered)
'1 year old',
'14 years old',
'7 years old ',
'16 years old ',
我该怎么办?
【问题讨论】:
-
df['Age'].str.startswith()是一个很好的起点,或者df['Age'].str.contains() -
使用
df['Age'] = [x for x in df['Age'] if not x.startswith('\d+')]我得到了这个 AttributeError: 'bool' object has no attribute 'startswith' -
你不能用正则表达式和
startswith,只能处理实际数据,可以这么说
标签: python regex pandas dataframe