如何重构代码以修复 159 行 python 代码中已贬值的 pandas '.append'?

问题描述 投票:0回答:1

我们引用一位想要将数据迁移到 Hubspot 的客户的话,现在我们正在处理需要规划的数据建模和数据库问题。

在规划迁移时,我们对 Hubspot 数据进行 RTM,我的团队成员发现了这段代码,它在几个方面都有帮助。然而,pandas .append 已被弃用,这意味着我们必须改变它的编写方式......取决于数据的结构。

我尝试重构它,将其移至 jupyter 笔记本中,而 ChatGPT 75% 无用,因此向 SO 寻求建议。

对于这么大的代码块,我有几个问题,这里列出了整个代码Hubspot社区数据

  1. 欢迎提出所有问题,希望这不是问一个愚蠢的问题,我不知道如何解决这个问题。
  2. 你如何看待整个 152 行代码并将其解构为更小的块?最好,每个函数只做 1 或 2 件事,但不要更多,这就是我希望做的。
  3. 现在 .append 不再可用,我该如何重构或调整下面的块代码,以便数据字典可以工作?由于 _append 不太可能是最有效的选择,我不确定如何或从哪里开始。

Hubspot 数据伪造者

# Loop to generate data for each company
    for _ in range(company_rows )
        company_name = faker.company()
        company_domain = faker.domain_name()
        company_industry = faker.random_element(["Technology", "Healthcare", "Finance", "Real Estate"])
        company_address = faker.address().replace('\n', ', ')
        company_country = country

        # Loop to generate data for each contact
        for _ in range(contacts_per_company )
            contact_firstname = faker.first_name()
            contact_lastname = faker.last_name()
            contact_email = faker.email()
            contact_phone = faker.phone_number()
            contact_address = faker.address().replace('\n', ', ')
            contact_country = country
            contact_function = faker.job()
            contact_department = faker.random_element(["Sales", "Marketing", "Human Resources", "Engineering"])

            # Append generated company and contact data to the lists in the dictionary
            data["company_name"].append(company_name)
            data["company_domain"].append(company_domain)
            data["company_industry"].append(company_industry)
            data["company_address"].append(company_address)
            data["company_country"].append(company_country)
           
            data["contact_firstname"].append(contact_firstname)
            data["contact_lastname"].append(contact_lastname)
            data["contact_email"].append(contact_email)
            data["contact_phone"].append(contact_phone)
            data["contact_address"].append(contact_address)
            data["contact_country"].append(contact_country)
            data["contact_function"].append(contact_function)
            data["contact_department"].append(contact_department)

            # Generate deal and product data
            data["deal_name"].append(f'Deal-{faker.uuid4()}')
            data["deal_stage"].append(faker.random_element(["Appointment Scheduled", "Qualified To Buy", "Presentation Scheduled", "Decision Maker Brought-In"]))
            data["deal_amount"].append(faker.random_int(min=1000, max=50000))
            data["deal_type"].append(faker.random_element(["New Business", "Existing Business"]))
            data["deal_source"].append(faker.random_element(["Direct Traffic", "Organic Search", "Paid Search", "Social Media"]))
            data["close_date"].append((datetime.today() + timedelta(days=faker.random_int(min=1, max=90))).date())

            # Generate product data
            data["product_name"].append(f'Product-{faker.uuid4()}')
            data["product_price"].append(faker.random_int(min=10, max=1000))
            data["product_description"].append(faker.catch_phrase())
            data["product_sku"].append(faker.random_int(min=10000, max=99999))
            data["product_quantity"].append(faker.random_int(min=1, max=100))

            # Generate ticket data
            data["ticket_title"].append(f'Ticket-{faker.uuid4()}')
            data["ticket_status"].append(faker.random_element(["New", "Waiting on contact", "Waiting on us", "Closed"]))
            data["ticket_priority"].append(faker.random_element(["Low", "Medium", "High"]))

    # Convert the data dictionary to a pandas DataFrame
    df = pd.DataFrame(data)
    return df

python pandas data-migration hubspot
1个回答
0
投票

解决标题中有关使用已弃用方法的问题

DataFrame.append
。问题出在您未包含在问题中的代码部分。您应该编辑问题以包含它,以防链接将来失效。

# Create an empty DataFrame to hold the generated data
result = pd.DataFrame()
for country in g7_countries:
    df = generate_data(country)
    # Append the data for each country to the result DataFrame
    result = result.append(df)

您可以使用

concat
:

result_list = []

for country in g7_countries:
    df = generate_data(country)
    result_list.append(df)

result = pd.concat(result_list)

此代码会生成假数据,您可能只想将其用于测试。我不会费心去优化它。

© www.soinside.com 2019 - 2024. All rights reserved.