简体   繁体   中英

Replace the values of multiple rows with the values of another row based on a condition in Pandas

表说明

Hi, I want to replace the values of the columns A, B, C, D with the values where region = '' for the a unique value for the year 2011. For example, the unique column with the value 1 for 2011 will replace its 3, 4, 9, 8 values with 6, 6, 6, 6; this approach would then be applied to the unique values 2 and 3. Afterwards the rows where region = '' would be dropped.

Other questions related to this don't have the answers I am looking for. I have tried using loc but to no avail. Thanks

I think you need this:

df=df.groupby(['unique','year']).agg('last').reset_index()

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