基于两列添加索引+排序列值+条件

2024-05-20 01:07:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图添加一个基于两列的索引(在我的例子中是单个和集群)+第三列的排序值(totalPrice)

所以我有一个数据集,有三列-个体、集群和totalPrice。你知道吗

     individual  cluster  totalPrice  totalTripDurationMinutes
0       9710556        1      180.82                       140
1       9710556        0      202.32                       145
2       9710556        0      180.82                       140
3       9710535        7      729.44                       460
4       9710535        7      729.44                       640
5       9710535        7      702.60                       355
6       9710535        7      685.82                       300
7       9710535        7      685.82                       480
8       9710535        7      669.44                       520
9       9710535        7      669.44                       580
10      9710535        7      669.44                       700

我想做的是,对于每个个体和每个集群,我想找到totalPrice条目的数量,这些条目由当前totalPrice控制。 结果如下所示:

     individual  dominationCount  cluster  totalPrice  totalTripDurationMinutes
0       9710556     0                1      180.82                       140
1       9710556     0                0      202.32                       145
2       9710556     1                0      180.82                       140
3       9710535     0                7      729.44                       460
4       9710535     0                7      729.44                       640
5       9710535     1                7      702.60                       355
6       9710535     2                7      685.82                       300
7       9710535     2                7      685.82                       480
8       9710535     3                7      669.44                       520
9       9710535     3                7      669.44                       580
10      9710535     3                7      669.44                       700

你知道怎么在熊猫身上做吗?你知道吗


Tags: 数据目的数量排序集群条目individual例子
3条回答

您可以定义一个名为check_price的函数:

def check_price(x):
    #sort values of the prices and get only unique elements
    prices = x.sort_values(ascending=False).unique()
    #find index of of each price in the sorted prices to get the dominated count
    dominate =  [np.where(prices==val)[0] for val in x]
    return dominate

然后使用groupbytransform

df['dominatedCount'] = df.groupby(['individual', 'cluster'])['totalPrice'].transform(check_price)
df

    individual  cluster totalPrice  totalTripDurationMinutes    dominatedCount
0   9710556       1        180.82      140                              0.0
1   9710556       0        202.32      145                              0.0
2   9710556       0        180.82      140                              1.0
3   9710535       7        729.44      460                              0.0
4   9710535       7        729.44      640                              0.0
5   9710535       7        702.60      355                              1.0
6   9710535       7        685.82      300                              2.0
7   9710535       7        685.82      480                              2.0
8   9710535       7        669.44      520                              3.0
9   9710535       7        669.44      580                              3.0
10  9710535       7        669.44      700                              3.0

使用^{}methos='dense'并减去1

df['dominatedCount'] = (df.groupby(['individual', 'cluster'])['totalPrice']
                          .rank(ascending=False, method='dense')
                          .astype(int)
                          .sub(1))
print (df)
    individual  cluster  totalPrice  totalTripDurationMinutes  dominatedCount
0      9710556        1      180.82                       140               0
1      9710556        0      202.32                       145               0
2      9710556        0      180.82                       140               1
3      9710535        7      729.44                       460               0
4      9710535        7      729.44                       640               0
5      9710535        7      702.60                       355               1
6      9710535        7      685.82                       300               2
7      9710535        7      685.82                       480               2
8      9710535        7      669.44                       520               3
9      9710535        7      669.44                       580               3
10     9710535        7      669.44                       700               3

这里有一个非常复杂的方法:

result = df.merge(df.merge(df.merge(df[['individual',
                                        'cluster',
                                        'totalPrice']].drop_duplicates(),
                                    on=['individual',
                                        'cluster'],
                                    suffixes=('',
                                              '_new'),
                                    how='left'))
                    .query('totalPrice<totalPrice_new')
                    .drop('totalPrice_new',
                          axis=1)
                    .drop_duplicates()
                    .groupby(['individual',
                              'cluster',
                              'totalPrice'],
                             as_index=False)
                    .count()
                    .rename(columns={'totalTripDurationMinutes': 'dominationCount'}),
                  how='left', on=['individual', 'cluster', 'totalPrice']).fillna(0)

结果是:

    individual  cluster  totalPrice  totalTripDurationMinutes  dominationCount
0      9710556        1      180.82                       140              0.0
1      9710556        0      202.32                       145              0.0
2      9710556        0      180.82                       140              1.0
3      9710535        7      729.44                       460              0.0
4      9710535        7      729.44                       640              0.0
5      9710535        7      702.60                       355              1.0
6      9710535        7      685.82                       300              2.0
7      9710535        7      685.82                       480              2.0
8      9710535        7      669.44                       520              3.0
9      9710535        7      669.44                       580              3.0
10     9710535        7      669.44                       700              3.0

相关问题 更多 >