1

I have a df with 365 rows × 8 columns.

enter image description here

Columns are the following ['tave', 'tmax', 'tmin', 'vp', 'rhmax', 'rhmin', 'pp', 'gust']
So there are 365 measures of 'tave' during the year.
I want to add new column called 'next day tave' in the beginning of df.

How to do it?

My attempt is the following:

# let's add new column with "next day tave"
# "next day tave" is the value of next day
# e.g row 0, tave=-13.535, next tave=-5.791
 
next_tave_val = df.values[1:, 0:1]
print(next_tave_val.shape)
(364, 1)

# "next day tave" is 364 and we want to add that column to df that is 365 rows
# so need to add one more value to make it 365
# to append as 2d array

next_tave_val = np.append(next_tave_val, [[0]], axis=0)
print(next_tave_val.shape)

# now let's add 'next tave' column to df

df = df.insert(0,'next tave', next_tave_val)
print(df.shape)

Returns an error:

AttributeError: 'NoneType' object has no attribute 'shape'

What did I do wrong?

1
  • You can look at the examples in the documentation of df.insert to see what you did wrong. Commented Jan 31, 2022 at 4:53

1 Answer 1

3

There's actually a function designed for this very purpose: shift:

df['next day tave'] = df['tave'].shift(1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.