Extract the values, if the part of string in one column matches with part of string in another column

If you have two columns with strings and you want to extract the values from one column if the part of the string in that column matches with part of the string in another column, you can use a conditional statement along with string manipulation functions.


Here's an example using Python:


bash

Copy code

import pandas as pd


# sample data

df = pd.DataFrame({

    'col1': ['apple', 'banana', 'cherry', 'date', 'elderberry'],

    'col2': ['apple pie', 'banana split', 'cherry pie', 'date roll', 'elderberry juice']

})


# extract values if part of string in col1 matches with part of string in col2

df['result'] = [val.split()[0] if val.startswith(df.loc[i, 'col1']) else None for i, val in df['col2'].iteritems()]


print(df)

This code creates a dataframe with two columns col1 and col2. The result column is populated based on the values in col1 and col2. If the string in col2 starts with the value in col1, the first word of the string in col2 is extracted and stored in the result column. If the string in col2 does not start with the value in col1, the value in the result column is set to None.


You can modify this code to fit your specific requirements by changing the string manipulation functions and the conditional statement.



Post a Comment

Previous Post Next Post