简体   繁体   中英

Pandas counting how many times value changes for a specific index

I'm looking for a way that I can count how many times the price of a product changed overtime. It's not simply counting the unqiue values because I also need to count a change when the price is set back to the old one.

Here is a quick overview from what I have:

IN: df['price'].xs(486186506)

OUT:
Grabbed                   Price
2019-12-06 13:29:05       199
2019-12-13 14:17:41       199
2020-01-25 11:01:39       199
2020-01-25 11:04:46       199
2020-01-25 12:00:41    186.18
2020-01-25 12:02:26    186.18
2020-01-25 12:05:57    186.18
2020-01-25 12:11:07    186.18
2020-01-31 07:56:06       199
2020-01-31 07:57:00       199
2020-01-31 07:59:15       199
2020-02-14 16:29:14       229
2020-02-15 16:25:14       229
2020-02-18 16:25:15       199
Name: price, dtype: object

So what you can see in here is that the price changed 4 times.

199 -> 186.15

186.15 -> 199

199 -> 229

229 -> 199

I have around 25k unique indexes in my dataframe and I need to do this for each index.

There is MultiIndex , so use DataFrameGroupBy.diff for differences per first level, then not equal by Series.ne , sum per first level, convert to integers and last subtract 1 because first values of differences are missing values:

df1 = (df.groupby(level=0)['price'].diff()
         .ne(0)
         .sum(level=0)
         .astype(int)
         .sub(1)
         .reset_index(name='count'))
print (df1)
          id  count
0  486186506      4

Or compare shifted values per groups by DataFrameGroupBy.shift with original for not equal and then solution is same like above:

df1 = (df['price'].ne(df.groupby(level=0)['price'].shift())
                  .sum(level=0)
                  .astype(int)
                  .sub(1)
                  .reset_index(name='count'))
print (df1)
          id  count
0  486186506      4

Sample data:

print (df)
                                price
id        Grabbed                    
486186506 2019-12-06 13:29:05  199.00
          2019-12-13 14:17:41  199.00
          2020-01-2511:01:39   199.00
          2020-01-25 11:04:46  199.00
          2020-01-25 12:00:41  186.18
          2020-01-25 12:02:26  186.18
          2020-01-25 12:05:57  186.18
          2020-01-25 12:11:07  186.18
          2020-01-31 07:56:06  199.00
          2020-01-31 07:57:00  199.00
          2020-01-31 07:59:15  199.00
          2020-02-14 16:29:14  229.00
          2020-02-15 16:25:14  229.00
          2020-02-18 16:25:15  199.00

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM