2017-03-15 16 views
0

多くの繰り返し値の単純なベクトルを作成したいと思います。複数の繰り返し番号のRシリーズに相当するパンダ

numbers = pd.Series([np.repeat(1,5), np.repeat(2,4), np.repeat(3,3)]) 
numbers 
0 [1, 1, 1, 1, 1] 
1  [2, 2, 2, 2] 
2   [3, 3, 3] 
dtype: object 

におけるRと同等何:私はパンダとnumpyのを使ってPythonでこれを行うにしようと、私は全く同じものを得ることはありません、

> numbers <- c(rep(1,5), rep(2,4), rep(3,3)) 
> numbers 
[1] 1 1 1 1 1 2 2 2 2 3 3 3 

しかし:これはRで簡単です。 Python?

+1

以下に相当するRは 'rep(1:3、5:3) 'です。 – lmo

答えて

1

はちょうどあなたがnp.repeat

np.repeat([1, 2, 3], [5, 4, 3]) 

array([1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]) 

それとも純粋な形であなたがRに何をやったか複製する、と述べたpd.Series

pd.Series(np.repeat([1, 2, 3], [5, 4, 3])) 

0  1 
1  1 
2  1 
3  1 
4  1 
5  2 
6  2 
7  2 
8  2 
9  3 
10 3 
11 3 
dtype: int64 

np.repeatと一緒にnp.concatenateを使用することであると使用方法を調整します。それは私がやってお勧めするものではありません。

np.concatenate([np.repeat(1,5), np.repeat(2,4), np.repeat(3,3)]) 

array([1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]) 
関連する問題