Reputation: 1922
I have the following dataframe
col1 col2
0 str9 val1
1 str8 val2
2 str4 val3
3 str2 val4
4 unknown1 asdvstr1df
5 random1 teststr2test
and the following list
strings = ['str1', 'str2', 'str3', 'str4']
I want to replace the value in column 1 if there is a match at any point in the string in column two with the given list, strings.
Note: col2 can have the values of the list 'string' inside the string, or at either end as well.
Currently I am doing this with an ugly and slow nested loop:
import pandas as pd
data_file = pd.DataFrame(data = ([['str9', 'val1'], ['str8', 'val2'], ['str4','val3'] , ['str2', 'val4'] , ['unknown', 'asdvstr1df'] , ['unknown', 'teststr2test']] ), columns = (['col1', 'col2']), dtype = str)
strings = ['str1', 'str2', 'str3', 'str4']
for value in range(data_file.shape[0]):
for text in strings:
if (str(data_file.col2[value]).find(text) != -1):
data_file.loc[value, 'col1'] = text
I am unsure how to improve upon this slow process. How can I make this run faster than the current O(nm) time (n is size of the data_file, m the size of the list called strings)?
The output should be:
col1 col2
0 str9 val1
1 str8 val2
2 str4 val3
3 str2 val4
4 str1 asdvstr1df
5 str2 teststr2test
Upvotes: 2
Views: 130
Reputation: 153460
IIUC,
x = '(' + '|'.join(strings)+ ')'
df.assign(col1 = df.col2.str.extract(x, expand=False).combine_first(df.col1))
Output:
col1 col2
0 str9 val1
1 str8 val2
2 str4 val3
3 str2 val4
4 str1 asdvstr1df
5 str2 teststr2test
Upvotes: 5
Reputation: 323226
You can using replace
twice with regex
d1=dict(zip(strings,[1,2,3,4]))
d2=dict(zip([1,2,3,4],strings))
df.loc[df.col1=='unknown','col1']=df.col2.replace(d1,regex=True).replace(d2)
df
Out[970]:
col1 col2
0 str9 val1
1 str8 val2
2 str4 val3
3 str2 val4
4 str1 asdvstr1df
5 str2 teststr2test
Upvotes: 3
Reputation: 101
Try this:
data_file["col1"] = data_file["col2"].apply(lambda y:strings[[True if x in y
else False for x in strings ].index(True)] if any([True if x in y else False
for x in strings ]) else y)
print(data_file)
OUTPUT:
col1 col2
0 val1 val1
1 val2 val2
2 val3 val3
3 val4 val4
4 str1 asdvstr1df
5 str2 teststr2test
Upvotes: 1