【问题标题】:How to use group by and return rows with null values如何使用 group by 并返回具有空值的行
【发布时间】:2016-04-02 01:03:47
【问题描述】:

我有一个关于电子邮件和购买的数据集,如下所示。

Email          Purchaser    order_id    amount 
a@gmail.com    a@gmail.com    1         5
b@gmail.com         
c@gmail.com    c@gmail.com    2         10
c@gmail.com    c@gmail.com    3         5

我想求数据集中的总人数、购买人数以及总订单数和总收入金额。我知道如何通过SQL 使用left join 和聚合函数来做到这一点,但我不知道如何使用Python/pandas 来复制它。

对于Python,我尝试使用pandasnumpy

table1 = table.groupby(['Email', 'Purchaser']).agg({'amount': np.sum, 'order_id': 'count'})

table1.agg({'Email': 'count', 'Purchaser': 'count', 'amount': np.sum, 'order_id': 'count'})

问题是 - 它只返回具有顺序的行(第一行和第三行)而不是其他行(第二行)

Email          Purchaser      order_id    amount 
a@gmail.com    a@gmail.com    1           5
c@gmail.com    c@gmail.com    2           15

SQL 查询应如下所示:

SELECT count(Email) as num_ind, count(Purchaser) as num_purchasers, sum(order) as orders , sum(amount) as revenue
    FROM
        (SELECT Email, Purchaser, count(order_id) as order, sum(amount) as amount
        FROM table 1 
        GROUP BY Email, Purchaser) x 

如何在Python 中复制它?

【问题讨论】:

  • 购买者是 'Na 还是 NaN' ?如果是,您可以使用 'dropna()' 来获取结果
  • 欢迎来到 StackOverflow - 你可以阅读tour

标签: python numpy pandas dataframe missing-data


【解决方案1】:

它现在没有在 pandas 中实现 - see

所以一个糟糕的解决方案是将NaN 替换为某个字符串,然后在agg 之后替换回NaN

table['Purchaser'] = table['Purchaser'].replace(np.nan, 'dummy')
print table
         Email    Purchaser  order_id  amount
0  a@gmail.com  a@gmail.com         1       5
1  b@gmail.com          NaN       NaN     NaN
2  c@gmail.com  c@gmail.com         2      10
3  c@gmail.com  c@gmail.com         3       5

table['Purchaser'] = table['Purchaser'].replace(np.nan, 'dummy')
print table
         Email    Purchaser  order_id  amount
0  a@gmail.com  a@gmail.com         1       5
1  b@gmail.com        dummy       NaN     NaN
2  c@gmail.com  c@gmail.com         2      10
3  c@gmail.com  c@gmail.com         3       5

table1 = table.groupby(['Email', 'Purchaser']).agg({'amount': np.sum, 'order_id': 'count'})
print table1
                         order_id  amount
Email       Purchaser                    
a@gmail.com a@gmail.com         1       5
b@gmail.com dummy               0     NaN
c@gmail.com c@gmail.com         2      15

table1 = table1.reset_index()
table1['Purchaser'] = table1['Purchaser'].replace('dummy', np.nan)
print table1
         Email    Purchaser  order_id  amount
0  a@gmail.com  a@gmail.com         1       5
1  b@gmail.com          NaN         0     NaN
2  c@gmail.com  c@gmail.com         2      15

【讨论】:

    猜你喜欢
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 2011-12-30
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多