Back to Community
Notebook - DataFrame.replace not working

Hi,
here is my problem

result = run_pipeline(make_pipeline(), start_date='2015-01-01', end_date = '2015-02-02')
result.head()

result.replace(["Equity(6583 [RTN])"],
['Equity(12691 [LMT])'], inplace=True)
<<<
I want to substitute the word [RTN] with [LMT], but my DataFrame (result) doesn't change at all
Am I using the wrong command?

5 responses

Hi Francesco,

Do you mean you wish to change the column name? If so, try pandas.DataFrame.rename() (documentation here). pandas.DataFrame.replace() will replace the actual values of the DataFrame, not the column or row names.

In this case, I think result.rename(columns={'Equity(6583 [RTN])': 'Equity(12691 [LMT])', inplace=True) should work. Let me know!

Hi George

I've tried
result.rename(columns={'Equity(6583 [RTN])':'Equity(12691 [LMT])'}, inplace=True)

but it didn't work :(
Look at this notebook

Suggestions?

Loading notebook preview...
Notebook previews are currently unavailable.

Hi Francesco,

Sorry, didn't realize that the sids were the index, not the column. Attached is the notebook with the proper reindexing. Unfortunately, there wasn't an easy way to do this, but with enough pandas hacking you can get what you want. I'd suggest reading up on pandas - especially the MultiIndex class, and the stack() and unstack() functions. They're tricky, but can be very helpful when dealing with Pipeline results.

The better question though, is why you want to replace the index like this in the first place. Surely it would be better to simply run the pipeline for the two stocks you wanted?

GH

Loading notebook preview...
Notebook previews are currently unavailable.

Thnaks George!
It solved my problem..on notebooks.. but now on Algorithm it seems not working :(
I can attach my algorithm here (line 92 - 95, I've copied your lines)

It seems I'm always missing something!

Clone Algorithm
0
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 596bdebd6a08a15252990c72
There was a runtime error.

Some missing info
1) Also in this case, I'm trying to substitute "Equity(6583 [RTN]" with "Equity(12691 [LMT])"

2) You asked me why.
I've found there is a strong correlation between RTN and LMT. Moreover, alpha factors of RTN can effectively predict LMT.
I've found several couples of company highly correlated.
So, if I can substitute RTN with LMT directly inside the dataframe it will be easier to tell my algo to buy the correct tag

F