Generate dataframe columns where each column is shift(-1) of previous column

Viewed 50

I want to create 44 dataframe columns based on TAZ_1720 such that each column is shift(-1) of the previous column.

How can I do it instead of writing it 44 times?

df['m1']=df['TAZ_1270'].shift(-1)
df['m2']=df['m1'].shift(-1)
df['m3']=df['m2'].shift(-1)

table

1 Answers

Use DataFrame.assign with a dict comprehension.

Here is a minimal example with 4 shifts:

df = pd.DataFrame({'TAZ_1270': [100047, 100500, 100488, 100099]})

#    TAZ_1270
# 0    100047
# 1    100500
# 2    100488
# 3    100099
df = df.assign(**{f'm{i}': df['TAZ_1270'].shift(-i) for i in range(1, 5)})

#    TAZ_1270        m1        m2        m3  m4
# 0    100047  100500.0  100488.0  100099.0 NaN
# 1    100500  100488.0  100099.0       NaN NaN
# 2    100488  100099.0       NaN       NaN NaN
# 3    100099       NaN       NaN       NaN NaN

Re: questions in comments

Why use **?

DataFrame.assign normally accepts the format df.assign(col1=foo, col2=bar, ...). When we use ** on a dict in a function call, it automatically unpacks the dict's 'col1': foo, 'col2': bar, ... pairs into col1=foo, col2=bar, ... arguments.

Why use f?

This is f-string syntax (introduced in python 3.6). f'm{i}' is just a more concise version of 'm' + str(i).

Related