2016-10-10 158 views
0

我嘗試在循環中使用pandas.Series()。append(),但返回的是與此方法相同的系列。我需要這種方法來返回一個重複值的熊貓系列。由num決定的重複次數。在循環中追加熊貓系列

def expand(ser, num): 
sers = ser 
x = 0 
while(x < num): 
    sers.append(sers).reset_index(drop=True) 
    x+=1 
return sers 
+0

一切都縮進正確: –

+0

請參閱[XY問題(http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem);它可能適用於這種情況。 –

回答

0

是不是有理由不想使用pandas.Series.repeat

import pandas as pd 
import numpy as np 

s = pd.Series(np.random.randn(3), index=['a', 'b', 'c']) 

num = 3 

repeated_s = s.repeat(num) 
repeated_s 

a 1.445775 
a 1.445775 
a 1.445775 
b -1.672416 
b -1.672416 
b -1.672416 
c 1.551762 
c 1.551762 
c 1.551762 
dtype: float64