I am looking to replace the value in a column depending on whether it starts with a number or not. The loop below returns NaNs for each of the values.
My desired output is:
novalue (First 6 digits)
novalue
one (Extract what is in parentheses)
two
df = pd.DataFrame({'VALUE': ['novalue1', 'novalue2', '22n(one)', '22n(two)',
'completed', 'none'],})
import re
for row in df.iterrows():
try:
value=row[1].VALUE
if re.search("^[a-zA-Z]", value) is not None:
df['VALUE'] = df['VALUE'].str[:6]
else:
df['VALUE'] = df['VALUE'].str.extract(r'\((.*)\)', expand=False)
except :
print(value, ' : Unsuccessful')
df