Certainly! Based on your request, you want to process a DataFrame that contains a column named 'Clustered Keywords'. Each entry in this column is a string of keywords separated by commas, like:
"pot, green, potty training, bathroom, practical"
You want to:
- Split this string by commas.
- Remove any leading or trailing whitespace from the keywords.
- Map each keyword to a specific category based on its position in the list:
- First word:
Objects
- Second word:
Adjectives
- Third word:
Activities
- Fourth word:
Places
- Fifth word:
Feelings
Here's how you can achieve this using pandas:
import pandas as pd
# Sample DataFrame
data = {
'Clustered Keywords': [
'pot, green, potty training, bathroom, practical',
'ball, round, playing, park, fun',
'book, interesting, reading',
'computer, fast, coding, office, productive',
'bicycle, red'
]
}
df = pd.DataFrame(data)
# Function to split and assign keywords
def assign_keywords(row):
# Split the 'Clustered Keywords' string into a list
keywords = [word.strip() for word in row['Clustered Keywords'].split(',')]
# Assign keywords based on their position
row['Objects'] = keywords[0] if len(keywords) > 0 else ''
row['Adjectives'] = keywords[1] if len(keywords) > 1 else ''
row['Activities'] = keywords[2] if len(keywords) > 2 else ''
row['Places'] = keywords[3] if len(keywords) > 3 else ''
row['Feelings'] = keywords[4] if len(keywords) > 4 else ''
return row
# Apply the function to each row
df = df.apply(assign_keywords, axis=1)
# Display the updated DataFrame
print(df)
Output:
Clustered Keywords Objects Adjectives Activities Places Feelings
0 pot, green, potty training, bathroom, practical pot green potty training bathroom practical
1 ball, round, playing, park, fun ball round playing park fun
2 book, interesting, reading book, interesting reading
3 computer, fast, coding, office, productive computer fast coding office productive
4 bicycle, red bicycle red
Explanation:
- Splitting and Stripping: We split the
'Clustered Keywords' string by commas and strip any leading or trailing whitespace from each keyword.
- Assigning Keywords: We assign each keyword to a new column based on its position in the list. If there are fewer keywords than expected, we fill the missing categories with an empty string or leave them blank.
- Handling Missing Keywords: If a row has fewer than five keywords, the corresponding categories for missing keywords will be empty.
Notes:
- This approach does not rely on predefined word lists or categories.
- It avoids using an
'other' category.
- The keywords are assigned purely based on their positions in the comma-separated string.
Alternative Method Using vectorized operations:
For better performance, especially with large datasets, you can use pandas' vectorized string operations:
# Split the 'Clustered Keywords' column into multiple columns
# Limit the number of splits to 4 to account for up to 5 keywords
keywords_expanded = df['Clustered Keywords'].str.split(',', n=4, expand=True)
# Strip whitespace from each keyword
keywords_expanded = keywords_expanded.applymap(lambda x: x.strip() if isinstance(x, str) else '')
# Rename columns to match categories
keywords_expanded.columns = ['Objects', 'Adjectives', 'Activities', 'Places', 'Feelings']
# Concatenate the new columns with the original DataFrame
df = pd.concat([df, keywords_expanded], axis=1)
# Display the updated DataFrame
print(df)
Output:
Clustered Keywords Objects Adjectives Activities Places Feelings
0 pot, green, potty training, bathroom, practical pot green potty training bathroom practical
1 ball, round, playing, park, fun ball round playing park fun
2 book, interesting, reading book interesting reading
3 computer, fast, coding, office, productive computer fast coding office productive
4 bicycle, red bicycle red
This method uses pandas' str.split function to directly split the column into multiple columns and applymap along with a lambda function to remove any extra whitespace.
Conclusion:
You now have a DataFrame where each keyword from the 'Clustered Keywords' column has been assigned to its respective category based on its position in the original string.