You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将BeautifulSoup提取的Python数据输出转换为DataFrame行列格式

Convert Scraped Data to Pandas DataFrame

Got it, let's tweak your code to turn that line-by-line output into a structured pandas DataFrame. Here's a straightforward approach:

First, make sure you've got pandas installed and imported. Instead of printing each entry right away, we'll collect the scraped data into lists, then convert those lists into a DataFrame for easier analysis.

Modified Code

import requests
from bs4 import BeautifulSoup
import pandas as pd  # Import pandas for DataFrame functionality

response = requests.get(source_data)
soup = BeautifulSoup(response.text, "html.parser")
States = soup.find_all('div', class_='card bg-darker p-3 mb-3')

# Initialize empty lists to store our scraped data
state_list = []
vaccine_rate_list = []

for item in States:
    # Clean up text with .strip() to remove extra whitespace from HTML
    state_name = item.find(class_='fw-bold fs-5 mb-2').text.strip()
    vaccinated_per = item.find(class_='col-3 text-end fs-5 ff-s text-success').text.strip()
    
    # Add each entry to our lists
    state_list.append(state_name)
    vaccine_rate_list.append(vaccinated_per)

# Create the DataFrame with descriptive column names
vaccination_data = pd.DataFrame({
    "State": state_list,
    "Vaccination Rate": vaccine_rate_list
})

# Optional: Convert percentage strings to numeric values (for calculations)
vaccination_data["Vaccination Rate (Numeric)"] = vaccination_data["Vaccination Rate"].str.replace("%", "").astype(float)

# View the final DataFrame
print(vaccination_data)

What This Does:

  • We use empty lists to gather all state names and vaccination rates as we scrape them, instead of printing immediately.
  • The pd.DataFrame() function turns our lists into a table with clear column headers.
  • The optional step converts percentage strings (like "80.24%") to float values (80.24), which lets you run numerical operations (like averages or sorting) on the data.

Example Output DataFrame:

State Vaccination Rate  Vaccination Rate (Numeric)
0      Flanders           80.24%                        80.24
1      Wallonia           70.00%                        70.00
2     Brussels           56.73%                        56.73
3  Ostbelgien            65.11%                        65.11

Now you've got a structured dataset you can export to CSV, filter, or use for further analysis!

内容的提问来源于stack exchange,提问作者zzstar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 03:03:13