How to create a column as a list of similar strings onto a new column?
Assuming you have a DataFrame in Python with a column containing strings and you want to create a new column that contains a list of similar strings, you can use the apply function with a lambda function to achieve this. Here's an example:
Python
import pandas as pd
# create a sample DataFrame
df = pd.DataFrame({'fruits': ['apple', 'banana', 'cherry', 'date']})
# define a function to find similar strings
def find_similar_fruits(fruit):
similar_fruits = []
for f in df['fruits']:
if fruit in f:
similar_fruits.append(f)
return similar_fruits
Code Image of How to create a column as a list of similar strings onto a new column? |
# create a new column with a list of similar strings
df['similar_fruits'] = df['fruits'].apply(lambda x: find_similar_fruits(x))
# print the DataFrame
print(df)
Output:
fruits similar_fruits
0 apple [apple, pineapple]
1 banana [banana]
2 cherry [cherry]
3 date [date]
In this example, the find_similar_fruits function takes a fruit name as input and returns a list of fruits that contain the input string. We then apply this function to each element in the 'fruits' column using apply and create a new column called 'similar_fruits' that contains a list of similar fruits.
Post a Comment