2017-12-12 5 views
1

を交換する私は、次のデータフレームがあります。numpyの:条件付きnp.whereは

'customer_id','transaction_dt','product','price','units' 
1,2004-01-02 00:00:00,thing1,25,47 
1,2004-01-17 00:00:00,thing2,150,8 
2,2004-01-29 00:00:00,thing2,150,25 
3,2017-07-15 00:00:00,thing3,55,17 
3,2016-05-12 00:00:00,thing3,55,47 
4,2012-02-23 00:00:00,thing2,150,22 
4,2009-10-10 00:00:00,thing1,25,12 
4,2014-04-04 00:00:00,thing2,150,2 
5,2008-07-09 00:00:00,thing2,150,43 
5,2004-01-30 00:00:00,thing1,25,40 
5,2004-01-31 00:00:00,thing1,25,22 
5,2004-02-01 00:00:00,thing1,25,2 

を私は次のプロセスを持っている:

start_date_range = pd.date_range('2004-01-01 00:00:00', '12-31-2017 00:00:00', freq='30D') 
end_date_range = pd.date_range('2004-01-30 23:59:59', '12-31-2017 23:59:59', freq='30D') 

tra = df['transaction_dt'].values[:, None] 
idx = np.argmax(end_date_range.values > tra, axis=1) 

df['window_start_dt'] = np.take(start_date_range, idx) 
df['window_end_dt'] = end_date_range[idx] 

はしかし、私はDFとの問題を解決するためにnp.whereを使用する必要があります[ 'window_start_dt']を次の条件で置き換えます。

'transaction_dt' <= 'window_start_dt'の場合は、前の日時の値をstart_date_rangeに設定します。

+0

らしいです。ハングアップは何ですか? 'numpy.where'ドキュメントについてはっきりしないことはありますか?データフレームの前の行にアクセスすることに関するあなたの質問は何ですか? –

+0

ありがとう、私は近いと思います。私は、np.whereを条件付きで 'window_start_dt'の値をstart_date_rangeのような配列やリストから条件的に置き換える方法についてはっきりしていません – Pylander

+0

[numpy'のオンラインドキュメント](https://docs.scipy.org/) doc/numpy-1.13.0/reference/generated/numpy.where.html)を参照してください。 –

答えて

1

私はあなたが使用することができると思う:あなたが問題を解決する方法を知っているよう

tra = df['transaction_dt'].values[:, None] 
idx = np.argmax(end_date_range.values > tra, axis=1) 

sdr = start_date_range[idx] 
m = df['transaction_dt'] < sdr 
#change value by condition with previous 
df["window_start_dt"] = np.where(m, start_date_range[idx - 1], sdr) 

df['window_end_dt'] = end_date_range[idx] 
print (df) 
    customer_id transaction_dt product price units window_start_dt \ 
0    1  2004-01-02 thing1  25  47  2004-01-01 
1    1  2004-01-17 thing2 150  8  2004-01-01 
2    2  2004-01-29 thing2 150  25  2004-01-01 
3    3  2017-07-15 thing3  55  17  2017-06-21 
4    3  2016-05-12 thing3  55  47  2016-04-27 
5    4  2012-02-23 thing2 150  22  2012-02-18 
6    4  2009-10-10 thing1  25  12  2009-10-01 
7    4  2014-04-04 thing2 150  2  2014-03-09 
8    5  2008-07-09 thing2 150  43  2008-07-08 
9    5  2004-01-30 thing1  25  40  2004-01-01 
10   5  2004-01-31 thing1  25  22  2004-01-01 
11   5  2004-02-01 thing1  25  2  2004-01-31 
+0

これは素晴らしいことですが、将来はnp.whereを設定する方法がわかります!最後の調整は1回だけです。 5行目の比較演算子は '<'ではなく '<='でなければなりません。結果の行10は、2004-01-01のトランザクション日付の場合は2004-01-01 - 2004-02-29のウィンドウで終了します。私は微調整の後に受け入れ、他の質問からも参照します。 – Pylander

+0

私は電話のみで、出力を変更することはできません。しかし、「=」は削除されました。コメントありがとう。 – jezrael

0

あなたは次のようにnumpy.where()を使用することができます。このようなものについて

numpy.where(df['transaction_dt'] <= df['window_start_dt'], *operation when True*, *operation when False*) 
0

何?

# get argmax indices 
idx = df.transaction_dt.apply(lambda x: np.argmax(end_date_range > x)).values 
# define window_start_dt 
df = df.assign(window_start_dt = start_date_range[idx]) 

# identify exceptions 
mask = df.transaction_dt.le(df.window_start_dt) 
# replace with shifted start_date_rage 
df.loc[mask, "window_start_dt"] = start_date_range[idx - 1][mask] 

出力:

customer_id transaction_dt product price units window_start_dt 
0    1  2004-01-02 thing1  25  47  2004-01-01 
1    1  2004-01-17 thing2 150  8  2004-01-01 
2    2  2004-01-29 thing2 150  25  2004-01-01 
3    3  2017-07-15 thing3  55  17  2017-06-21 
4    3  2016-05-12 thing3  55  47  2016-04-27 
5    4  2012-02-23 thing2 150  22  2012-02-18 
6    4  2009-10-10 thing1  25  12  2009-10-01 
7    4  2014-04-04 thing2 150  2  2014-03-09 
8    5  2008-07-09 thing2 150  43  2008-07-08 
9    5  2004-01-30 thing1  25  40  2004-01-01 
10   5  2004-01-31 thing1  25  22  2004-01-01 
11   5  2004-02-01 thing1  25  2  2004-01-31