0

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!

Matthias Gallagher
  • 475
  • 1
  • 7
  • 20

1 Answers1

2

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
Chris
  • 29,127
  • 3
  • 28
  • 51
  • Thank you for your answer! The percentage change part works really well. However, I would also like df2 to display the Account column. Is there any way to do so? I appreciate your help! – Matthias Gallagher Aug 13 '20 at 03:30
  • 1
    @MatthiasGallagher I've edited to reflect the change ;) – Chris Aug 13 '20 at 03:52
  • Thanks again, for the edit! I've realised that the output is an array instead of a dataframe, and that the column names are not aligned. Can these be fixed too? Much appreciated!! – Matthias Gallagher Aug 13 '20 at 03:57
  • 1
    @MatthiasGallagher How did you get the array? which line produced it? besides, for the alignment, its because the index has the name _account_. If you want it to behave as a column, try `df2.reset_index()` – Chris Aug 13 '20 at 04:00
  • 1
    Problem solved. It is the print(df2) line. Thank you very much for your help! – Matthias Gallagher Aug 13 '20 at 04:06