将列表传递给熊猫系列作为索引

el323

我可以将列表作为索引传递给熊猫系列吗?

我有以下数据框:

d = {'no': ['1','2','3','4','5','6','7','8','9'], 'buyer_code': ['Buy1', 'Buy2', 'Buy3', 'Buy1', 'Buy2', 'Buy2', 'Buy2', 'Buy1', 'Buy3'], 'dollar_amount': ['200.25', '350.00', '120.00', '400.50', '1231.25', '700.00', '350.00', '200.25', '2340.00'], 'date': ['22-01-2010','14-03-2010','17-06-2010','13-04-2011','17-05-2011','28-01-2012','23-07-2012','25-10-2012','25-12-2012']}
df = pd.DataFrame(data=d)
df

    buyer_code  date        dollar_amount   no
0   Buy1        22-01-2010  200.25          1
1   Buy2        14-03-2010  350.00          2
2   Buy3        17-06-2010  120.00          3
3   Buy1        13-04-2011  400.50          4
4   Buy2        17-05-2011  1231.25         5
5   Buy2        28-01-2012  700.00          6
6   Buy2        23-07-2012  350.00          7
7   Buy1        25-10-2012  200.25          8
8   Buy3        25-12-2012  2340.00         9

转换为浮点数以进行汇总

pd.options.display.float_format = '{:,.4f}'.format
df['dollar_amount'] = df['dollar_amount'].astype(float)

通过频率和美元获得最重要的买家:

注意:在这里,我只获得排名前2位的买家,在实际示例中,我可能最多需要40位买家。

xx = df.groupby('buyer_code').agg({'dollar_amount' : 'mean', 'no' : 'size'})
xx['frqAmnt'] = xx['no'].values * xx['dollar_amount'].values
xx = xx['frqAmnt'].nlargest(2)
xx

buyer_code
Buy2       2,631.2500
Buy3       2,460.0000
Name: frqAmnt, dtype: float64

分组买家及其购买日期:

zz = df.groupby(['buyer_code'])['date'].value_counts().groupby('buyer_code').head(all)
zz

buyer_code  date      
Buy1        2010-01-22    1
            2011-04-13    1
            2012-10-25    1
Buy2        2010-03-14    1
            2011-05-17    1
            2012-01-28    1
            2012-07-23    1
Buy3        2010-06-17    1
            2012-12-25    1
Name: date, dtype: int64

现在,我想将我的顶级Buyer_codes传递给我,zz sereis以仅获取与那些买家相对应的交易数据。

我该怎么做?我在这里可能走错了路,但是请帮我。

耶斯列尔

我认为您需要:

a = zz[zz.index.get_level_values(0).isin(xx.index)]
print (a)
buyer_code  date      
Buy2        14-03-2010    1
            17-05-2011    1
            23-07-2012    1
            28-01-2012    1
Buy3        17-06-2010    1
            25-12-2012    1
Name: date, dtype: int64

对于订单需求reindex

a = zz[zz.index.get_level_values(0).isin(xx.index)].reindex(xx.index, level=0)

对于所有日期buyer_code

b = a.reset_index(name='a').groupby('buyer_code')['date'].apply(list).reset_index()
print (b)
  buyer_code                                              date
0       Buy2  [14-03-2010, 17-05-2011, 23-07-2012, 28-01-2012]
1       Buy3                          [17-06-2010, 25-12-2012]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将列表或系列作为一行附加到熊猫DataFrame吗?

是否可以将列作为参数传递给ODER BY子句?

如何使用Apply将列作为参数传递给类

将列作为参数传递给 Pandas groupby apply 函数

将列表作为参数传递给命令

为什么可以将列表作为值的索引却不能将列作为索引呢?

将pandas系列作为一列添加到多索引的DataFrame填充级别

将系列作为行应用于所有行中具有相同系列的熊猫数据框

如何将整个列作为参数传递给tldextract函数?

在R中如何将列作为参数传递给strsplit?

如何将另一整列作为参数传递给pandas fillna()

Sidekiq:如何将队列作为变量传递给工作程序

如何将除一列以外的所有列作为参数传递给setkey()?

如何将矩阵列作为参数传递给.apply函数?

以整洁的方式将多列作为分组变量传递给 UDF

如何将多列作为参数传递给pyspark写repartition()

将熊猫系列转换为保留索引的列表

返回一个项目系列作为熊猫的元组

提供不可对齐的布尔系列作为索引器

使用 React 将索引作为状态传递给组件

如何将参数作为索引传递给jq?

使用系列索引作为列将熊猫系列转换为数据框

熊猫:FutureWarning:将类似列表的内容传递给.loc或[]

'将喜欢列表的人传递给熊猫中的.loc

大熊猫可以使用列作为索引吗?

熊猫按月汇总,有2列作为索引

在熊猫数据框中使用多列作为索引

将列表索引传递给用户定义的函数 python

无法将 StringIndexer 作为列表传递给模型管道阶段