Pandas Dataframe: Update values in a certain columns for last n rows

Lorenz

In the example below, I want to update column C for the last 3 rows to the value 0.

Source Dataframe

A B C D E
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

Target Dataframe

A B C D E
1 1 1 1 1
2 2 2 2 2
3 3 0 3 3
4 4 0 4 4
5 5 0 5 5

I tried something like

df.tail(3)['C']=0

but it does not work. Any idea?

rafaelc

You can settle for

df.loc[df.tail(3).index, 'C'] = 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extract rows where the lists of columns contain certain values in a pandas dataframe

Pandas dataframe - update the values of certain rows based on the condition of a groupby object

Color only certain rows and columns of a Pandas DataFrame

How to collectively set the values of multiple columns for certain selected rows for Pandas dataframe?

How to select rows in pandas dataframe that only have certain values in some columns?

pandas dataframe get rows when list values in specific columns meet certain condition

Values from rows to columns of a pandas dataframe

Update rows in Pandas Dataframe based on the list values

How to get the last N rows of a pandas DataFrame?

How to get the last N rows of a pandas DataFrame?

Update columns with duplicate values from the DataFrame in Pandas

How to keep only the last entry based on certain columns in a Pandas dataframe?

Change values in pandas dataframe based on values in certain columns

Pandas reshape dataframe every N rows to columns

Select rows in pandas dataframe if all the columns contain certain pattern

pandas: divide a dataframe based on a condition in certain columns and rows

How to get rows that has certain columns containing same values in pandas?

How to convert dataframe columns with list of values into rows in Pandas DataFrame

Mean of last N rows of pandas dataframe if the previous rows meet a condition

Generating n amount new rows on a pandas dataframe based off values given in other columns

How to sort values of just certain rows in pandas dataframe?

How to replace certain rows by shared column values in pandas DataFrame?

Changing certain values in multiple columns of a pandas DataFrame at once

Select rows from a DataFrame based on last characters of values in a column in pandas

Update values in pandas columns based on values from another DataFrame

Create new columns for last n rows in dataframe by group dplyr

Getting the MAE values of columns in pandas dataframe with last column

How to drop columns of Pandas DataFrame with zero values in the last row

Pandas Dataframe -update dynamic rows and columns without loops