简体   繁体   English

将行值转换为下一行,但保持某些列值相同

[英]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? 我想将第1到第3行的内容复制到第3到第6行,但是我想对第4到第6行保留Forecast_price和Forecast_ail列的值相同。我该怎么做?

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 我想你打算复制第4至6行

You can use: 您可以使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas 使用一些条件列值保留相同 ID 的最新行 - Pandas keep the latest rows for the same ID with some conditional column values Pandas:如果两行或多行在特定列中具有相同的值,则获取其计数并添加到下一行 - Pandas: If two or more rows have same values in particular column then get its count and add to next row 如何将列值添加到熊猫中同一列的下一行 - how to add the column values to the next row of the same column in pandas 在numpy数组列中插入一个值,并保持相同的大小/行值 - Insert a value in a numpy array column and keep the same size / row values 是否有一些有效的方法来找到满足下一个条件行的条件? - Are there some efficient ways to find row(s) meeted conditons which referred to the values in next some rows? 对于每一行,在列中找到所有具有相同值的行 - For each row, find all rows with same values in column 根据下一行查找上一行值 - Finding previous row values based on next rows 如何根据另一列中的重复值添加一列中的行,并最终将第一行保留在python中? - How to add rows in one column based on repeated values in another column , and finally keep the first row in python? 使用pandas在csv文件的同一行上填充下一列值的行中的空值 - Fill empty values from a row with the value of next column on the same row on csv file with pandas 保留列值重复的最后一行 - Keep last row where column values are duplicates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM