Let's say I have the following empty df called df_pnl
, after calling it it produces the following result:
Empty DataFrame
Columns: [PnL, Type of Trade, ROI, Cumulative ROI, Cumulative Investment]
Index: []
If you run df_pnl.index[-1:]
, it produces the following output:
Index([], dtype='object', name='End Date')
Now, there is another df called df_chosen_trading_pair
that shows the following data as output when called:
Close Price
End Date
2022-06-25 02:29:59.999 11.21
2022-06-25 02:59:59.999 11.19
2022-06-25 03:29:59.999 11.25
2022-06-25 03:59:59.999 11.37
2022-06-25 04:29:59.999 11.34
...
2022-06-27 04:59:59.999 10.96
2022-06-27 05:29:59.999 10.80
2022-06-27 05:59:59.999 10.82
2022-06-27 06:29:59.999 10.84
2022-06-27 06:59:59.999 10.88
[106 rows x 1 columns]
If you run df_chosen_trading_pair.index[-1]
, it produces the following output:
'2022-06-27 06:59:59.999'
If you run type(df_chosen_trading_pair.index[-1])
, it produces the following output:
str
I need to add the value of df_chosen_trading_pair.index[-1]
a df_pnl.index[-1:]
to get the following result when calling df_pnl
:
PnL Type of Trade ROI Cumulative ROI Cumulative Investment
End Date
2022-06-27 06:59:59.999 NaN NaN NaN NaN NaN
I tried the following lines and none of them worked:
In[124]:
df_pnl.index[0:] = df_chosen_trading_pair.index[-1]
TypeError: Index does not support mutable operations
In[125]:
df_pnl["End Date"][-1] = df_chosen_trading_pair.index[-1]
KeyError: 'End Date'
Can you help me, please?
Good day,
To do so, the
dataframe
void must have the columns you needThen you can use
pandas.DataFrame.reindex
to add the index of the otherdataframe
With a test file "sample.csv" with the data you put in your question, create the following example:
Printing
df_e
we get:Oops, I think I accidentally found my solution:
Generate:
Also, I realized that if for example I want to add a new row using the penultimate index value of
df_chosen_trading_pair
, I simply have to change the value inside the array, like this:Generate: