在Python中操纵数据帧

时间:2015-08-25 13:27:07

标签: python pandas ipython

我尝试使用以下结构但是对于多个城市并且已经列出了每天的城市(如果我确实有访问多个城市的脚本而不是问题将被解决,并且还包括城市的日期。 ..我可能要求很多,但我不想为这个特殊问题建立一个新包...请告诉我):     enter image description here

我可以请求帮助吗?我现在拥有的:行/索引基于日期和基于城市/国家的列......

希望基于日期和列的行看起来像图片。我确实使用wunderground / weather API。所以下面的脚本不允许我做任何修改。我尝试了所有,仍然按照我预期的结果,也许是

  

来自wunderpython import wunderground

是问题所在。

for city in wg['London, United Kingdom', 'Birmingham, United Kingdom', 'Leeds, United Kingdom', 'Glasgow, United Kingdom', 'Sheffield, United Kingdom', 'Bradford, United Kingdom', 'Liverpool, United Kingdom', 'Edinburgh, United Kingdom', 'Manchester, United Kingdom', 'Paris, France', 'Marseille, France', 'Lyon, France', 'Toulouse, France', 'Nice, France', 'Nantes, France', 'Strasbourg, France', 'Montpellier, France', 'Bordeaux, France', 'Lille, France', 'Grenoble, France', 'Dijon, France']:
    ds[city.name] = []
    for day in city.history['2015.08.10':'2015.08.24']:
       dailysummary = day['dailysummary'][0]        
       if dailysummary['date']['mon']+'.'+dailysummary['date']['mday']+'.'+dailysummary['date']['year'] not in x:
          x.append(dailysummary['date']['mon']+'.'+dailysummary['date']['mday']+'.'+dailysummary['date']['year'])
       ds[city.name].append(float(dailysummary['humidity']))


df = DataFrame(ds, x)

enter image description here

2 个答案:

答案 0 :(得分:1)

如果我能正确理解您的问题,您可以reset your index然后set a new index栏:

df = df.reset_index().set_index('date')

答案 1 :(得分:0)

如果有人遇到同样的问题,请发布问题的答案。

from wunderpython import wunderground
wg = wunderground.Wunderground('your_key')
from pandas import *
x = []
ds = {
    'meantempi':[],
    'maxtempi':[],
    'mintempi':[],
    'humidity':[],
    'city':[],
}

for  city in wg['London, United Kingdom','Birmingham, United Kingdom','Lyon, France']:

for day in city.history['2015.08.25':'2015.08.31']:
    dailysummary = day['dailysummary'][0]        
    if dailysummary['date']['year']+'.'+dailysummary['date']['mon']+'.'+dailysummary['date']['mday'] not in x:
        x.append(dailysummary['date']['year']+'/'+dailysummary['date']['mon']+'/'+dailysummary['date']['mday'])
        ds['meantempi'].append(float(dailysummary['meantempi']))
        ds['maxtempi'].append(float(dailysummary['maxtempi']))
        ds['mintempi'].append(float(dailysummary['mintempi']))
        ds['humidity'].append(float(dailysummary['humidity']))
        ds['city'].append(city.name)
df = DataFrame(ds, x)

enter image description here