I need to do the following steps on two columns -A and B- of my df and output the result in C:
1) check if value from B is present in A -on row, at any position
2) if present but in another format then remove
3) add value from B in A and output in C
A B C
tshirt for women TSHIRT TSHIRT for women
Zaino Estensibile SJ Gang SJ Gang Zaino Estensibile
Air Optix plus AIR OPTIX AIR OPTIX plus
Workaround with concatenation between A and B and duplicate removal:
Version1
def uniqueList(row):
words = str(row).split(" ")
unique = words[0]
for w in words:
if w.lower() not in unique.lower() :
if w.lower()not in my_list:
unique = unique " " w
return unique
df["C"] = df["C"].apply(uniqueList)
Version2
sentences = df["B"] .to_list()
for s in sentences:
s_split = s.split(' ') # keep original sentence split by ' '
s_split_without_comma = [i.strip(',') for i in s_split]
# method 1: re
compare_words = re.split(' |-', s)
# method 2: itertools
compare_words = list(itertools.chain.from_iterable([i.split('-') for i in s_split]))
method 3: DIY
compare_words = []
for i in s_split:
compare_words = i.split('-')
# strip ','
compare_words_without_comma = [i.strip(',') for i in compare_words]
start to compare
need_removed_index = []
for word in compare_words_without_comma:
matched_indexes = []
for idx, w in enumerate(s_split_without_comma):
if word.lower() in w.lower().split('-'):
matched_indexes.append(idx)
if len(matched_indexes) > 1: # has_duplicates
need_removed_index = matched_indexes[1:]
need_removed_index = list(set(need_removed_index))
# keep remain and join with ' '
print(" ".join([i for idx, i in enumerate(s_split) if idx not in need_removed_index]))
# print(sentences)
print(sentences)
None of this are working properly as is not the best way to approach.
CodePudding user response:
Using sets, get strings in A
not in B
.Put these strings in column C
as a set
df['C'] = [(set(a).difference(b)) for a, b in zip(df['A'].str.upper().str.split('\s'), df['B'].str.upper().str.split('\s'))]
Strip of the new column C
the set brackets and the comma and concatenate
with column B
if B is a substring of A. If not, just concatenate B and A.
Code below;
df['C']= np.where([a in b for a, b in zip(df.B.str.lower(),df.A.str.lower())], df['B'] ' ' df['C'].str.join(',').str.replace(',',' ').str.lower(), df['B'] ' ' df['A'])
print(df)
Output
A B C
0 tshirt for women TSHIRT TSHIRT for women
1 Zaino Estensibile SJ Gang SJ Gang Zaino Estensibile
2 Air Optix plus AIR OPTIX AIR OPTIX plus
CodePudding user response:
Here's a solution using regular expressions, assuming that df
is the name of the dataframe.
So the idea is simple, if B has something in A, replace it with B's value. Else return string B A.
import re
def create_c(row):
if re.sub(row['B'], row['B'], row['A'], flags=re.IGNORECASE) == row['A']:
return row['B'] row['A']
re.sub(row['B'], row['B'], row['A'], flags=re.IGNORECASE)
df['C'] = df.apply(create_c, axis=1)