How can I delete the rows whose 'Recipe Start' or 'Recipe End' columns contain letters/symbols?:
Title Recipe Start Recipe End Year
0 cookie 500 400 2013
1 soup 600 650 1993
2 rice 30 5A-EE 1810
3 ramen v vii 2001
4 pate EP56 2KP 2005
Output:
Title Recipe Start Recipe End Year
0 cookie 500 400 2013
1 soup 600 650 1993
What I've tried:
import pandas as pd
import re
raw = {'Title': ['cookie', 'soup', 'rice', 'ramen', 'pate'],
'Recipe Start': [500, 600, 30, 'v', 'EP56'],
'Recipe End': [400, 650, '5A-EE', 'vii', '2KP'],
'Year': [2013, 1993, 1810, 2001, 2005]}
data = pd.DataFrame(raw, columns = ['Title', 'Recipe Start', 'Recipe End', 'Year'])
data['Recipe Start'] = data[[(re.search(r'[a-zA-Z]', x)==False) for x in data['Recipe Start'].astype(str)]]
print(data)
Option 1
to_numeric
based filtering
df[df.iloc[:, 1:3].apply(pd.to_numeric, errors='coerce').notnull().all(1)]
Title Recipe Start Recipe End Year
0 cookie 500 400 2013
1 soup 600 650 1993
Option 2
str.isdigit
df[df['Recipe Start'].str.isdigit() & df['Recipe End'].str.isdigit()]
Title Recipe Start Recipe End Year
0 cookie 500 400 2013
1 soup 600 650 1993
If these are object columns, you'll need to convert to str
beforehand, and then the str
accessor methods can be used on these columns:
i = df['Recipe Start'].astype(str).str.isdigit()
j = df['Recipe End'].astype(str).str.isdigit()
df[i & j]
Option 3
applymap
+ str.isdigit
df[df.iloc[:, 1:3].astype(str).applymap(str.isdigit).all(1)]
Title Recipe Start Recipe End Year
0 cookie 500 400 2013
1 soup 600 650 1993
Incase you are looking for a regex
solution then you can use replace and dropna ie
data.loc[data[['Recipe Start','Recipe End']].replace('[A-Za-z]',np.nan,regex=True).dropna().index]
Title Recipe Start Recipe End Year
0 cookie 500 400 2013
1 soup 600 650 1993
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.