python - Pandas Dataframe index from nested values in dictionary -


I am making a panda dataframe from the weather underground download historical weather data.

Import JSN import requests as imported as imported PDs, such as NP import dated date from the date time, date parse import paras address = "http://api.wunderground.com /api/7036740167876b59/history_20060405/q/CA/San_Francisco.json "r = get request (address) wu_data = r.json ()

Because I do not need all the data, I only use the list of observations. There are two elements in this list - the date and the UTCDT - which are actually dictionaries.

  df = pd.DataFrame.from_dict (wu_data ["history"] ["comments"])  

I have the 'beautiful' key inside the dictionary I want to index the dataframe created with the parsed date. I can access this value using the array index, but I can not understand how it can directly do without a loop. For example, for the 23rd element I print pretty_date time = parse (pretty_date) print time

  pretty_date = df ["date"] values ​​[23] ["beautiful"] >  

and get me

  11:56 PM PDT 05 Apr 2006 2006-04-05 23:56:00  

This is what I am doing

  g = lambda x: pars (x ["beautiful"]) df_dates = pd.DataFrame.from_dict (df ["date"]) Df Index = df_date ["date"]. Applicable (g)  

DF has been redone now. At this point I can remove the columns I do not need.

Is there a more direct way to do this?

Please note that sometimes there are many comments for the same date, but I work with data cleaning, duplicate, etc. in a different part of the code.

Since beautiful organized dtype only objects You can only take them to a list and get indexed. Not sure that this is what you want:

  #include, `r.json` without ()` wu_data = r.json df = pd.DataFrame.from_dict (wu_data [" # Df.index = [purse (df ["date"] [n] "#" ["comments"]) # Using an understanding of the index list, "beautiful" to get the DF ["date"] in the object. ["Beautiful"]) in the range n (len (df))] df.index & class; Class 'pandas.tseries.index.DatetimeIndex' & gt; [2006-04-05 00:56:00, ..., 2006-04-05 23:56:00] Length: 24, Freak: None, Timezone: None  

Hope it helps.


Comments