2017-03-14 7 views
-1

私は次元(95、)の時間データを持っています。私は次元(95,3)の配列を作成する年、月、日を抽出する次のコードを書いた。ただし、次のコードでは次元(285)の配列を作成できます。最初の列が年、2番目の列 - 月、1番目の最後の列を表す次元(95,3)の新しい時間配列を作成するにはどうすればよいですか。例えばPythonでデータを追加する

newtime = np.array([]) 
for i in range(len(time)): 
    a = seconds_since_jan_1_1993_to_datetime(time[i]) 
    time_year = float(a.strftime("%Y")) 
    time_mon = float(a.strftime("%m")) 
    time_day = float(a.strftime("%d")) 
    newtime = np.append(newtime, np.array([time_year, time_mon, time_day])) 

、私は、次の形式の出力をする要素のアレイ([725696054.99044609、725696056.99082708、725696058.99119401、...])

持つ入力配列を有する:

Col1 Col2 Col3 
2015.0 12.0 31.0 
2015.0 12.0 31.0 
2015.0 12.0 31.0 

あなたの提案や助けを楽しみにしています。

+0

は、それはリストのリストで値を収集するのが最善ですhttp://stackoverflow.com/q/42752203 – hpaulj

+0

参照してください。次に、最後に構造化配列またはデータフレームを作成します。 – hpaulj

答えて

0

私の提案は、データフレーム形式で作業することになります。

あなたのコードに簡単に修正は次のようになります。助け

newtime = pd.DataFrame([], columns=['year','month','day']) 

for i in range(len(time)): 
    a = seconds_since_jan_1_1993_to_datetime(time[i]) 
    time_year = float(a.strftime("%Y")) 
    time_mon = float(a.strftime("%m")) 
    time_day = float(a.strftime("%d")) 
    newtime.loc[len(newtime)] = [time_year, time_mon, time_day] 

希望!

0

データフレームは良い選択です。しかし、配列を保持したい場合は、単にnumpyのreshape()関数を使うことができます。

import numpy as np 

newtime = np.array([]) 

for i in range(12): 
    # Dummy data generated here, using floats like in the original post 
    time_year = float(2015.0) 
    time_mon = float(1.0*i) 
    time_day = float(31.0) 
    newtime = np.append(newtime,np.array([time_year, time_mon, time_day])) 

newtime = newtime.reshape((-1,3)) 

注変形関数の引数:ここでサンプルコードは、(1,3)に自動的に最初の寸法を計算し、第二の次元3を作るためにnumpyのを教えてくれます。さて、あなたはNEWTIMEを印刷する場合は、あなたが表示されるはずです。

[[ 2.01500000e+03 0.00000000e+00 3.10000000e+01] 
[ 2.01500000e+03 1.00000000e+00 3.10000000e+01] 
[ 2.01500000e+03 2.00000000e+00 3.10000000e+01] 
[ 2.01500000e+03 3.00000000e+00 3.10000000e+01] 
[ 2.01500000e+03 4.00000000e+00 3.10000000e+01] 
[ 2.01500000e+03 5.00000000e+00 3.10000000e+01] 
[ 2.01500000e+03 6.00000000e+00 3.10000000e+01] 
[ 2.01500000e+03 7.00000000e+00 3.10000000e+01] 
[ 2.01500000e+03 8.00000000e+00 3.10000000e+01] 
[ 2.01500000e+03 9.00000000e+00 3.10000000e+01] 
[ 2.01500000e+03 1.00000000e+01 3.10000000e+01] 
[ 2.01500000e+03 1.10000000e+01 3.10000000e+01]] 
関連する問題