Home > database >  Pandas dataframe column shift and add
Pandas dataframe column shift and add

Time:01-30

I would like to add a new column to existing dataframe. The new column needs to start with a constant value in the first row (-17.3 in the example below) and then consecutively add 0.15 to it for all 26000 rows. The new column should have following values in the end:

-17.3
-17.15
-17
-16.85
…
…
…
26000 rows

Is there any way to get this done without looping over all the rows?

Thanks,

Yoshiro

CodePudding user response:

You can construct the range like this:

# 26,000 numbers
# step of 0.15
# starting at -17.3
np.arange(26000) * 0.15 - 17.3

CodePudding user response:

Let's say your dataframe is named df, you can do it in the following way:

start_value = -17.3
increment_value = 0.15
new_column = [start_value   increment_value * i for i in range(df.shape[0])]
df['new_column'] = new_column

CodePudding user response:

Either use pandas.Series constructor with pandas.Series.cumsum :

N, S, F = len(df), -17.3, 0.15 # <- len(df) = 26000 in your case

df["approach_1"] = pd.Series([S]   [np.NaN]*(N-1)).fillna(F).cumsum()

Or simply go for numpy.arange as per @tdy :

df["approach_2"] = np.arange(S, S   N*F, F)

Output :

print(df)

       approach_1  approach_2
0          -17.30      -17.30
1          -17.15      -17.15
2          -17.00      -17.00
3          -16.85      -16.85
4          -16.70      -16.70
...           ...         ...
25995     3881.95     3881.95
25996     3882.10     3882.10
25997     3882.25     3882.25
25998     3882.40     3882.40
25999     3882.55     3882.55

[26000 rows x 2 columns]
  • Related