简体   繁体   中英

PYTHON How to add a column using numpy.where that includes data from the dataframe in the next row?

Im really new to python and would appreciate your help.

I'm trying to add a column to my data frame in python. I'm using the following:

df['capital']=np.where(df['year']!=1960,2,df['GDP'])

Except, when I write GDP what I really want is the GDP for the year 1961. Any idea how I can include that?

You can use .shift() to use the GDP value in the next row. By default a 1 is passed to .shift() , but you can also pass -1 or 2 , etc to compare up or down as many rows as you'd like:

df['capital'] = np.where((df['year'] != 1960), 2, df['GDP'].shift())

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