Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Shift down values by one row within a group

I have a Pandas dataframe, and I want to create a new column whose values are that of another column, shifted down by one row. The last row should show NaN.

The catch is that I want to do this by group, with the last row of each group showing NaN. NOT have the last row of a group "steal" a value from a group that happens to be adjacent in the dataframe.

My attempted implementation is quite shamefully broken, so I'm clearly misunderstanding something fundamental.

df['B_shifted'] = df.groupby(['A'])['B'].transform(lambda x:x.values[1:]) 
like image 914
jeffalstott Avatar asked Oct 09 '14 13:10

jeffalstott


People also ask

How do you drop a specific range of a row in pandas?

You can delete a list of rows from Pandas by passing the list of indices to the drop() method. In this code, [5,6] is the index of the rows you want to delete. axis=0 denotes that rows should be deleted from the dataframe.

How do I move row values in pandas?

shift() If you want to shift your column or subtract the column value with the previous row value from the DataFrame, you can do it by using the shift() function. It consists of a scalar parameter called period, which is responsible for showing the number of shifts to be made over the desired axis.

How do you shift a row down in Python?

Just use df. dropna() and it will drop all the NaN rows without you having to specify the number of rows to drop.

How do you drop rows with certain values?

One of the fastest ways to delete rows that contain a specific value or fulfill a given condition is to filter these. Once you have the filtered data, you can delete all these rows (while the remaining rows remain intact).


1 Answers

Newer versions of pandas can now perform a shift on a group:

df['B_shifted'] = df.groupby(['A'])['B'].shift(1) 

Note that when shifting down, it's the first row that has NaN.

like image 164
chrisaycock Avatar answered Sep 26 '22 00:09

chrisaycock