Если эти три случая являются исчерпывающими, вы можете использовать мое решение, мое решение использует комбинацию сопоставления регулярных выражений и разделения.
#the hard part
def my_scan(t):
#Split
#only '+' and 'and' are considered
cond = re.findall(r'(.+)(and|\+)(.+)' , t)
if len(cond):
t = [_.strip() for _ in cond[0]]
else:
t = [t]
#Process
#Case 1 'House': and
if 'and' in t:
t.remove('and')
#add 'House' to the second element
t[1] = re.split(' ', t[0])[0]+' '+t[1]
#Case 2 'Garage + Garden': + with numeral
elif '+' in t:
t.remove('+')
x = []
##check for numerals in front
for _ in t:
if (re.match(r'^\d+', _)):
temp = _[(re.match(r'^\d+', _)).end()+1:] #'garage'
#append by the number of numeral times
for i in range(int(re.match(r'^\d+', _)[0])):
x.append(temp+' '+str(i+1))
else:
x.append(_)
t = x
#Case 3 'Fridges': single word that ends with an s
else:
if (re.match(r'^[A-Za-z]+s$', t[0])):
t = t[0][:-1]
t = [t+' 1', t+' 2']
else:
t[0] = t[0]+' 1'
return t
#the easier part
def get_df(t):
output1 = []
output2 = []
for _ in t:
dummy = my_scan(_)
for i in range(len(dummy)):
output1.append(_)
output2.append(dummy[i])
df = pd.DataFrame({'var1':output1,'var2':output2})
return df
#test it
data = {'var1':['House A and B','2 Garage + Garden', 'Fridges']}
df = get_df(data['var1'])
print(df)
#bonus test
data1 = {'var1':['House AZ and PL','5 Garage + 3 Garden', 'Fridge']}
df = get_df(data1['var1'])
print(df)
Печатный вывод df из ваших исходных данных, data = {'var1':['House A and B','2 Garage + Garden', 'Fridges']}
.
var1 var2
0 House A and B House A
1 House A and B House B
2 2 Garage + Garden Garage 1
3 2 Garage + Garden Garage 2
4 2 Garage + Garden Garden
5 Fridges Fridge 1
6 Fridges Fridge 2
Вывод на выходе df из дополнительных тестовых данных, data1 = {'var1':['House AZ and PL','5 Garage + 3 Garden', 'Fridge']}
.
var1 var2
0 House AZ and PL House AZ
1 House AZ and PL House PL
2 5 Garage + 3 Garden Garage 1
3 5 Garage + 3 Garden Garage 2
4 5 Garage + 3 Garden Garage 3
5 5 Garage + 3 Garden Garage 4
6 5 Garage + 3 Garden Garage 5
7 5 Garage + 3 Garden Garden 1
8 5 Garage + 3 Garden Garden 2
9 5 Garage + 3 Garden Garden 3
10 Fridge Fridge 1