2016-04-22 2 views
0

したがって、私はこの辞書を、私がpandasを使用して変換したものに変更しようとしています。pandasデータフレームを使用してループを介して辞書に値を追加する

for key in dictionary: 
     TypeError: 'instancemethod' object is not iterable 


     i = pandas.date_range('20110101', '20151231') 
     df = pandas.DataFrame(dict(year = i.year, month = i.month, day = i.day)) 
     time_format = pandas.to_datetime(df.year*10000 + df.month*100 + df.day, format='%Y%m%d') 
     times = (time_format.to_dict) 

     def adding_to_dict(dictionary): 
      for key in dictionary: 
       dictionary[key].append(list_of_random) 
      return dictionary 

     print adding_to_dict(times) 

私は運ではまだ機能が、を使用するために、短い辞書を使用してみました。しかし、私はまだエラーメッセージが表示されます。私はタイムスタンプのこの新しい辞書に値のリスト/タプルを追加しようとしています。どんな助けでも大歓迎です。あなたのお時間をいただきありがとうございます

答えて

0

お試しあればprint(times)これは辞書ではありません。また、ディクショナリに値を追加する方法ではなく、辞書に追加しようとしています。あなただけですdict[key] = value

出力を{timestamp: values}とすると、これはうまくいくはずです。

import pandas 
import datetime 

i = pandas.date_range('20110101', '20151231') 
df = pandas.DataFrame(dict(year=i.year, month=i.month, day=i.day)) 
time_format = pandas.to_datetime(df.year * 10000 + df.month * 100 + df.day, format='%Y%m%d') 
times = dict(time_format) 
output = {} 
def adding_to_dict(dictionary): 
    for key, value in dictionary.items(): 
     output[datetime.datetime.strftime(value, "%Y-%m-%d")] = 'whatever' 

    return output 


print adding_to_dict(times) 

出力:

{'2013-09-08': 'whatever', '2014-01-30': 'whatever', '2012-12-03': 'whatever', '2014-03-21': 'whatever'} 
+0

これは大きな助けです。しかし、私は時間を見なくてもちょうど日付のフォーマットを維持するにはどうすればいいのだろうか? – user106279

+0

はあなたのためにそれを修正するコードを更新しました。 – Chris

関連する問題