简体   繁体   中英

How to add a new column to a sub-dataframe from pandas

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.

You can do:

apple_2 = apple.loc[:, 'High':'Close']

this will give you all the columns between 'High' and ' Close' (without close). But there are also other ways to column-slice a dataframe. You an check this question .

EDIT:

apple_2 = apple.loc[:, 'High':'Close']
#add a new column to apple_2
apple_2['new_column'] = np.arange(apple_2.shape[0]) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM