Matthias Gallagher
Matthias Gallagher

Reputation: 471

Python dataframe tracking the percentage change of two months columns. Input month manually

I'm trying something which seems difficult to me. It would be very great if anyone could help me with this multi-operation. First, I have a dataframe 'df' that monitors hundreds of user account statistics of a website. 'Account' column contains the account name, 'Users' column contains the number of users under each account, and there are twelve 'Logins in (Month)' columns that monitors the number of logins per account.

Sample df:

Account     Users     Logins in January     Logins in February      Logins in March     Logins in April
Nike         148            68                      94                   72                   87
Adidas       654           134                     192                    248                   324
Apple        43             23                      40                   32                    29
Tesla        864           651                      598                   691                  439

I would like the user of this operation to type in the month, for instance, March. Then the cell would return the percentage change of Logins from February to March in a dataframe format. Something like this:

Input:

Please input Month:  (This is where the user type in the month in interest)

Output:

Account     Logins in February      Logins in March     Percentage Change (%)
Nike              94                       72                    -23               
Adidas            192                      248                    29                
Apple              40                      32                    -20              
Tesla             598                      691                    16

Thanks again! I really appreciate any help on this!

Upvotes: 0

Views: 161

Answers (1)

Chris
Chris

Reputation: 29742

Use pandas.DataFrame.filter with pct_change:

input_ = "Mar"
df2 = df.set_index("Account").filter(regex="Feb|%s" % input_).copy()
df2["Percentage Change (%)"] = df2.pct_change(axis=1).iloc[:, -1].mul(100).round()
print(df2)

Output:

         Logins in February  Logins in March  Percentage Change (%)
Account                                                            
Nike                     94               72                  -23.0
Adidas                  192              248                   29.0
Apple                    40               32                  -20.0
Tesla                   598              691                   16.0

Upvotes: 2

Related Questions