Python - Pandas dataframe - iteration through a column -
i have pandas dataframe , add empty column (named nb_trades
). fill new column 5 5 increment. should column values 5 10 15 20 ...
doing below code assign same value (last value of i
) in whole column , that's not wanted:
big_df["nb_trade"]='0' in range(big_df.shape[0]): big_df['nb_trade']=5*(i+1)
can me?
use range
or np.arrange
:
df = pd.dataframe({'a':[1,2,3]}) print (df) 0 1 1 2 2 3 df['new'] = range(5, len(df.index) * 5 + 5, 5) print (df) new 0 1 5 1 2 10 2 3 15
df['new'] = np.arange(5, df.shape[0] * 5 + 5, 5) print (df) new 0 1 5 1 2 10 2 3 15
solution of john galt
comment:
df['new'] = np.arange(5, 5*(df.shape[0]+1), 5) print (df) new 0 1 5 1 2 10 2 3 15
Comments
Post a Comment