【问题标题】:Convert dictionaries in Pandas DataFrame cells to DataFrame columns将 Pandas DataFrame 单元格中的字典转换为 DataFrame 列
【发布时间】:2021-11-08 08:28:50
【问题描述】:

我正在使用以下code 从 timeanddate.com 抓取天气数据:

import requests, re, typing
from bs4 import BeautifulSoup as soup
import contextlib
import pandas as pd

def _remove(d:list) -> list:
    return list(filter(None, [re.sub('\xa0', '', b) for b in d]))

@contextlib.contextmanager
def get_weather_data(url:str, by_url = True) -> typing.Generator[dict, None, None]:
    d = soup(requests.get(url).text if by_url else url, 'html.parser')
    _table = d.find('table', {'id':'wt-his'})
    _data = [[[i.text for i in c.find_all('th')], *[i.text for i in c.find_all('td')]] for c in _table.find_all('tr')]
    [h1], [h2], *data, _ = _data
    _h2 = _remove(h2)
    yield {tuple(_remove(h1)):[dict(zip(_h2, _remove([a, *i]))) for [[a], *i] in data]}

city = 'https://www.timeanddate.com/weather/germany/berlin/historic?hd=20210905'
with get_weather_data(city) as weather:
    #print(weather)

    df = pd.DataFrame.from_dict(weather)
    df = df['Conditions']['Comfort']

    # keep only temp, wind speed, humidity
    df0 = pd.DataFrame()

    df0[['Time','Temp','Wind']].assign(**pd.DataFrame(df0.to_dict()).T)

我想将结果表转换为 Pandas DataFrame,其中每列显示每次的温度、风速等值。对于每个时间戳,所有数据都存储在一个单元格中的字典中,因此我尝试了post 中的建议,但到目前为止没有任何效果。有没有一种简单的 Pythonic 方法可以将每个 DataFrame 行中的这些字典条目转换为列?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    转换为列表,然后像 post 一样返回 DataFrame:

    with get_weather_data(city) as weather:
        #print(weather)
    
        df = pd.DataFrame.from_dict(weather)
        df = df['Conditions']['Comfort']
    
        df0 = pd.DataFrame(df.tolist())
    

    【讨论】:

      猜你喜欢
      • 2019-10-27
      • 2014-01-05
      • 2021-11-28
      • 1970-01-01
      • 2017-12-26
      • 2014-12-30
      相关资源
      最近更新 更多