Python noob here. Big respect to you all. Hope you can help.
I have two pandas dataframes, df1 and df2:
df1:
| style_gid | style_name | |
|---|---|---|
| 0 | 1202357372219055 | DIA |
| 1 | 1202357372219056 | WVU |
| 2 | 1202357372219057 | GWE |
df2:
| project_gid | project_name | |
|---|---|---|
| 0 | 1202318051699659 | Project Master |
| 1 | 1202318100865244 | TEST-DIA-LON-1 |
| 2 | 1202320766191762 | TEST-WVU-LONMUN-1 |
| 3 | 1202381389399069 | TEST-DIA-MUN-2 |
| 4 | 1202381389399070 | Custom Section |
And I have tried so many things, so many. I need to work out how to check project_name in each row of df2 to see if it matches with one of the style_name entries from df1. If that's true, to then somehow bring the style_name and style_id into df2, like in the below table.
The rules are that if the text in style_name is found anywhere in the text in 'project_name', that's a match and so that project should be assigned the relevant style.
For example, the project names above contain two examples of matches with 'DIA', and one example of a match with 'WVU':
TEST-DIA-LON-1
TEST-WVU-LONMUN-1
TEST-DIA-MUN-2
The dataframe of styles is a moving target, the list will change values and number of entries for style_name (i.e. we may end up using others 'ABC', 'XYX', whatever as well), but the search will always be for 3-letter strings, in capitals.
Anything that doesn't match a style gets a null or none, or 'Potato', I don't mind, I can handle that later.
I cannot work out how to perform this lookup/insert, despite finding several solutions to test the name. Please can someone help?
So the resulting dataframe df2 ends up looking like this:
| project_gid | project_name | style_gid | style_name | |
|---|---|---|---|---|
| 0 | 1202318051699659 | Project Master | None/Null | None/Null |
| 1 | 1202318100865244 | TEST-DIA-LON-1 | 1202357372219055 | DIA |
| 2 | 1202320766191762 | TEST-WVU-LONMUN-1 | 1202357372219056 | WVU |
| 3 | 1202381389399069 | TEST-DIA-MUN-2 | 1202357372219057 | DIA |
| 4 | 1202381389399070 | Custom Section | None/Null | None/Null |
Or to a new dataframe, just so long as each project gets the style it deserves.