简体   繁体   中英

Translate row values to next rows, but keep some column values the same

I have the following dataframe:

    date                   forecast_price   pool_price  forecast_ail    ail
1   2019-09-03 11:00:00     34.90           35.5        9964            9970
2   2019-09-03 12:00:00     34.95           36.6        10074           10078
3   2019-09-03 13:00:00     34.94           37.7        10130           10135
4   2019-09-03 14:00:00     50.90           NaN         9000            NaN
5   2019-09-03 15:00:00     60.95           NaN         10000           NaN
6   2019-09-03 16:00:00     70.94           NaN         12000           NaN

I would like to copy the contents of rows 1 to 3 onto rows 3 to 6, but I'd like to leave the forecast_price and forecast_ail column values the same for rows 4 to 6. How do I go about doing so?

Expected output:

        date                forecast_price  pool_price  forecast_ail    ail
1   2019-09-03 11:00:00     34.90           35.5        9964            9970
2   2019-09-03 12:00:00     34.95           36.6        10074           10078
3   2019-09-03 13:00:00     34.94           37.7        10130           10135
4   2019-09-03 14:00:00     50.90           35.5        9000            9970
5   2019-09-03 15:00:00     60.95           36.6        10000           10078
6   2019-09-03 16:00:00     70.94           37.7        12000           10135

I guess you meant to copy in rows 4 to 6

You can use:

df.loc[[4,5,6],['pool_price','ail']]=df.loc[[1,2,3],['pool_price','ail']]

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