Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

python - Pandas apply function to column taking the value of previous column

I have to create a timeseries using column values for computing the Recency of a customer.

The formula I have to use is R(t) = 0 if the customer has bought something in that month, R(t-1) + 1 otherwise.

I managed to compute a dataframe

    CustomerID  -1  0   1   2   3   4   5   6   7   8   9   10  11  12
0   17850   0   0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0
1   13047   0   0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0 1.0
2   12583   0   0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3   14688   0   0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0
4   15311   0   0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
3750    15471   0   1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0
3751    13436   0   1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0
3752    15520   0   1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0
3753    14569   0   1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0
3754    12713   0   1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0

In which there's a 0 if the customer has bought something in that month and one otherwise. The column names indicate a time period, with the column "-1" as a dummy column.

How can I replace the value in each column with 0 if the current value is 0 and with the value of the previous column + 1 otherwise?

For example, the final result for the second customer should be 0 1 0 0 1 0 0 1 0 1 0 1 2

I know how to apply a function to a column, but I don't know how to make that function use the value from the previous column.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just use apply function to iterate throw columns or rows of dataframe and do manipulation.

def apply_function(row):
    return [item if i == 0 else 0 if item == 0 else item+row[i-1] for i,item in enumerate(row)]

new_df = df.apply(apply_function, axis=1, result_type='expand')
new_df.columns = df.columns  # just to set previous column names

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...