Python Forum
Find a string from a column of one table in another table
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find a string from a column of one table in another table
#4
Something like this maybe?
import pandas as pd
from string import ascii_letters as letters
from random import choice, choices, randint


def find_supplier(description):
    """Return word if word in description matches a supplier code, else None."""
    intersection = set(description.split()) & suppliers
    return list(intersection)[0] if intersection else None


# Make some random table thing that we can use to search for words in the description
# that match a supplier code.
product_table = pd.DataFrame(
    [
        {
            "Product": i,
            "Supplier Code": choice("ABCDE"),
            "Description": " ".join(choices(letters, k=randint(5, 10))),
        }
        for i in range(100, 120)
    ]
)

# Get set of suppliers.
suppliers = set(product_table["Supplier Code"].values)

# Make supplier table.  Supplier table contains rows from product_table
# where one of the words in the description matches a supplier code.
supplier_table = product_table[["Description"]]
supplier_table["Product"] = supplier_table["Description"].map(find_supplier)
supplier_table = supplier_table[~supplier_table["Product"].isna()][
    ["Product", "Description"]
]
print(supplier_table)
Output:
Product Description 1 E T u V x Z E a k s 2 A K H a K P G z m l A 3 E Q L H E q J 5 B N X x b i B q D F M 8 D d U q K Y W I D 10 C U V H C f F n N z 14 C C o X u J 15 E D F e E Q u 18 B o f B P x O
This is easy to break up into individual supplier tables.
for supplier in suppliers:
    print(
        supplier,
        supplier_table[supplier_table["Product"] == supplier].reset_index(drop=True),
        sep="\n",
        end="\n\n",
    )
Output:
A Product Description 0 A d j A c S o F U 1 A o A w I W 2 A z s e j A 3 A c P R w Z M A V b D Product Description 0 D t u P r p R v G D O 1 D j P w D h v o m w C Product Description 0 C n j r C r R T B Product Description 0 B H O P B A c r C n 1 B B g Z r z 2 B r o y g u l B A E Product Description 0 E P E m t Z 1 E S E Y m F a K h Z T
Reply


Messages In This Thread
RE: Find a string from a column of one table in another table - by deanhystad - Sep-05-2023, 06:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  drawing a table with the status of tasks in each thread pyfoo 3 519 Mar-01-2024, 09:29 AM
Last Post: nerdyaks
  How to create a table with different sizes of columns in MS word pepe 8 1,850 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,569 Oct-06-2023, 04:32 PM
Last Post: cubangt
  dict table kucingkembar 4 844 Sep-30-2023, 03:53 PM
Last Post: deanhystad
  Going through HTML table with selenium emont 3 921 Sep-30-2023, 02:13 AM
Last Post: emont
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 833 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Using pyodbc&pandas to load a Table data to df tester_V 3 919 Sep-09-2023, 08:55 PM
Last Post: tester_V
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 849 Aug-09-2023, 05:51 PM
Last Post: Calab
  Color a table cell based on specific text Creepy 11 2,261 Jul-27-2023, 02:48 PM
Last Post: deanhystad
Information Showing trendline formula in a table per product Carlossxx 0 732 May-03-2023, 08:34 AM
Last Post: Carlossxx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020