I am starting to work with pandas, so this is probably a pretty obvious question, but I have been struggling with it for a while now and found no solution.
Consider this dataframe:
import pandas_datareader as pdr
apple = pdr.DataReader('AAPL', data_source='yahoo',
start=datetime.datetime(2013, 1, 1),
end=datetime.datetime(2020, 1, 1))
Now, I can add a new column to this dataframe simply doing:
apple['new_column'] = np.arange(apple.shape[0])
However, if I usse iloc
to extract a subdataframe and try to add a new column to the subdataframe:
apple_2 = apple.iloc[1:5,:]
apple_2['test2'] = np.arange(4)
I get the error message:
<stdin>:1: 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
What am I doing wrong and how am I supposed to do this in pandas? The error suggests using .loc
but I do not know how to use it to add new columns.