3
               col_A    vi_B    data_source index_as_date
2017-01-21  0.000000  0.199354         sat       2017-01-21
2017-01-22  0.000000  0.204250         NaN           NaT
2017-01-23  0.000000  0.208077         NaN           NaT
2017-01-27  0.000000  0.215081         NaN           NaT
2017-01-28  0.000000  0.215300         NaN           NaT

In the pandas dataframe above, I want to insert a row for 24th January 2017 with value of 0.01, 0.4, sat, NaT, how do I do that? I could use iloc and manually insert but I would prefer an automated solution which takes the datetime index into account

1 Answer 1

2

I think you need setting with enlargement with sort_index:

#if necessary convert to datetime
df.index = pd.to_datetime(df.index)
df['index_as_date'] = pd.to_datetime(df['index_as_date'])

df.loc[pd.to_datetime('2017-01-24')] = [0.01,0.4,'sat', pd.NaT]
df = df.sort_index()
print (df)

            col_A      vi_B data_source index_as_date
2017-01-21   0.00  0.199354         sat    2017-01-21
2017-01-22   0.00  0.204250         NaN           NaT
2017-01-23   0.00  0.208077         NaN           NaT
2017-01-24   0.01  0.400000         sat           NaT
2017-01-27   0.00  0.215081         NaN           NaT
2017-01-28   0.00  0.215300         NaN           NaT
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.