Reputation: 1
I'm trying to insert a new column in a DataFrame that holds other DataFrames inside.
I have Data for two ETFs, the SPY and DBC. I'm trying to insert a new set of Data in the SPY DataFrame, but I'm getting a warning message.
In the end what I'm looking for is to get for SPY, columns (Open, High, Low, Adj Close, Volume and MA).
Thanks,
Steve
import yfinance as yf
import pandas as pd
from datetime import date
import numpy as np
tickers = ['SPY','DBC']
start_date = "2000-01-01"
end_date = str(date.today())
df = pd.DataFrame()
df = yf.download(tickers,start=start_date,end=end_date, group_by='ticker')
df = df.fillna(method='ffill')
df.dropna(inplace=True)
df['DBC']['MA'] = np.nan
I get:
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
Upvotes: 0
Views: 65
Reputation: 69
df.loc[:,('DBC', 'MA')] = np.nan
As already your warning message says, you need to use .loc[row_indexer, col_indexer]
. For multileveled indexes or columns you need to specify the indexer as tuples.
Here is why you need to use loc.
Upvotes: 3