Convert this dataframe into a map of ids to representitives: fdata[["topicid",…
- User
Convert this dataframe into a map of ids to representitives:
f_data[["topic_id", "topic_representive"]] PyDev console: using IPython 8.31.0 Out[1]: topic_id topic_representive 0 1.0 שנה, קיבוץ, קהילה 2 2.0 נשק, מחבל, ירי, חייל 5 2.0 נשק, מחבל, ירי, חייל 6 2.0 נשק, מחבל, ירי, חייל 7 2.0 נשק, מחבל, ירי, חייל ... ... ... 7634 3.0 צהל, חייל, צבא, הודעה 7635 0.0 דלת, מיטה, חלון, ממד 7636 1.0 שנה, קיבוץ, קהילה 7637 1.0 שנה, קיבוץ, קהילה 7638 1.0 שנה, קיבוץ, קהילה [4585 rows x 2 columns]
- Assistant
To convert your dataframe into a map of topic_id to topic_representive, where each topic ID maps to its most representative (or unique) representative, you can use drop_duplicates and then convert it to a dictionary. Here’s the code: pythonCopyEdit# Drop duplicates to ensure one representative per topic_id id_to_representative = ( f_data[["topic_id", "topic_representive"]] .drop_duplicates(subset="topic_id") .set_index("topic_id")["topic_representive"] .to_dict() )
print(id_to_representative)
Example Output: pythonCopyEdit{ 0.0: "דלת, מיטה, חלון, ממד", 1.0: "שנה, קיבוץ, קהילה", 2.0: "נשק, מחבל, ירי, חייל", 3.0: "צהל, חייל, צבא, הודעה", ... }
Let me know if you want to handle cases where there are multiple different representatives per topic_id.