2016-07-19 7 views
6

私はPythonでdefaultdictから読んでいるパンダのデータフレームを持っていますが、いくつかのカラムの長さが異なります。ここでは、データがどのように見えるかです:from_dictを使用してパンダにNaNを追加する代わりにプレイング

Date  col1 col2 col3 col4 col5 
01-01-15 5  12  1  -15  10 
01-02-15 7  0  9  11  7 
01-03-15   6  1  2  18 
01-04-15   9  8  10 
01-05-15   -4    7 
01-06-15   -11    -1 
01-07-15   6    

そして、私はそうのようなNaN sの空白パッドにできる午前:

pd.DataFrame.from_dict(pred_dict, orient='index').T 

与える:しかし

Date  col1 col2 col3 col4 col5 
01-01-15 5  12  1  -15  10 
01-02-15 7  0  9  11  7 
01-03-15 NaN  6  1  2  18 
01-04-15 NaN  9  8  10  NaN 
01-05-15 NaN -4  NaN  7  NaN 
01-06-15 NaN -11  NaN -1  NaN 
01-07-15 NaN  6  NaN  NaN  NaN 

、何私は本当に最後に追加するのではなく、NaNを前に追加する方法を探しているので、データは次のようになります。

Date  col1 col2 col3 col4 col5 
01-01-15 NaN  12  NaN  NaN  NaN 
01-02-15 NaN  0  NaN -15  NaN 
01-03-15 NaN  6  NaN  11  NaN 
01-04-15 NaN  9  1  2  NaN 
01-05-15 NaN -4  9  10  10 
01-06-15 5  -11  1  7  7 
01-07-15 7  6  8  -1  18 

これを行う簡単な方法はありますか?

あなたは、このコードで辞書を再作成することができます

import pandas as pd 
from collections import defaultdict 

d = defaultdict(list) 
d["Date"].extend([ 
    "01-01-15", 
    "01-02-15", 
    "01-03-15", 
    "01-04-15", 
    "01-05-15", 
    "01-06-15", 
    "01-07-15" 
]) 
d["col1"].extend([5, 7]) 
d["col2"].extend([12, 0, 6, 9, -4, -11, 6]) 
d["col3"].extend([1, 9, 1, 8]) 
d["col4"].extend([-15, 11, 2, 10, 7, -1]) 
d["col5"].extend([10, 7, 18]) 

答えて

4

あなたearlier questionにitertoolsソリューションにほとんど変更:ここで

pd.DataFrame(list(itertools.zip_longest(*[reversed(i) for i in d.values()]))[::-1], columns=d.keys()).sort_index(axis=1) 
Out[143]: 
     Date col1 col2 col3 col4 col5 
0 01-01-15 NaN 12 NaN NaN NaN 
1 01-02-15 NaN  0 NaN -15.0 NaN 
2 01-03-15 NaN  6 NaN 11.0 NaN 
3 01-04-15 NaN  9 1.0 2.0 NaN 
4 01-05-15 NaN -4 9.0 10.0 10.0 
5 01-06-15 5.0 -11 1.0 7.0 7.0 
6 01-07-15 7.0  6 8.0 -1.0 18.0 
+1

私はこれが好きです+1 – piRSquared

+0

ええ、読みにくいですが、これは仕事を終わらせます。私が受け入れたものに近い答えを期待していましたが、それはそのように見えません。 – weskpga

+0

私はそれほど実際には好きではありませんでした。その辞書を何度反復しているか分かりません。 :) – ayhan

4

あなたはシリーズ/データフレームを誘導するためにSeries.shiftを使用することができます。残念ながら、ピリオドの配列を渡すことはできません。各列を整数値でシフトする必要があります。リバース

s = df.isnull().sum() 
for col, periods in s.iteritems(): 
    df[col] = df[col].shift(periods) 
2

辞書内のすべてのリスト:

for k, v in d.iteritems(): 
    d[k] = v[::-1] 

df = pd.DataFrame.from_dict(d, orient='index').T.set_index('Date').sort_index(1).sort_index().astype(float) 

enter image description here

+0

dtypesはこの1つを持つオブジェクトになるのはなぜ? – ayhan

+0

いいえ、考えられません!面白い。 – piRSquared

+0

@ayhan 'df = df.astype(float)'これを修正します。多分 'int'を保存しようとしていますか? – piRSquared

0

が使用するベクトル化アプローチですpd.DataFrame.from_dictは、tのデータフレームを取得します。彼はいつものケース。通常の2Dデータが利用可能になると、それを反転してマスクし、ベクトル化された方法で希望の出力データフレームに到達するのが容易になります。

実装は以下にリストされている -

# Get the normal case output 
df = pd.DataFrame.from_dict(d, orient='index').T 

# Use masking to flip and select flipped elements to re-create expected df 
colmask = df.columns!='Date' 
arr = np.array(df.ix[:,colmask].values, dtype=np.float).T 
mask = ~np.isnan(arr) 
out_arr = np.full(mask.shape,np.nan) 
out_arr[mask[:,::-1]] = arr[mask] 
df.ix[:,colmask] = out_arr.T 

サンプル実行 -

In [209]: d.values() 
Out[209]: 
[[-15, 11, 2, 10, 7, -1], 
[10, 7, 18], 
[12, 0, 6, 9, -4, -11, 6], 
[1, 9, 1, 8], 
[5, 7], 
['01-01-15', 
    '01-02-15', 
    '01-03-15', 
    '01-04-15', 
    '01-05-15', 
    '01-06-15', 
    '01-07-15']] 

In [210]: df 
Out[210]: 
    col4 col5 col2 col3 col1  Date 
0 NaN NaN 12 NaN NaN 01-01-15 
1 -15 NaN 0 NaN NaN 01-02-15 
2 11 NaN 6 NaN NaN 01-03-15 
3 2 NaN 9 1 NaN 01-04-15 
4 10 10 -4 9 NaN 01-05-15 
5 7 7 -11 1 5 01-06-15 
6 -1 18 6 8 7 01-07-15 
関連する問題