File size: 786 Bytes
3e53b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pandas as pd


async def map_results(results: list[dict], mapping: dict[str, str]) -> pd.DataFrame:
    values = list(mapping.values())
    if "title" not in values:
        raise ValueError("title is required in the mapping")
    if "link" not in values:
        raise ValueError("link is required in the mapping")
    if "body" not in values:
        raise ValueError("body is required in the mapping")
    mapped_data = [
        {mapping.get(key, key): value for key, value in result.items()}
        for result in results
    ]
    result = {"title": [], "link": [], "body": []}
    for data in mapped_data:
        result["title"].append(data["title"])
        result["link"].append(data["link"])
        result["body"].append(data["body"])
    return pd.DataFrame(result)