aggregate

Pandas pivot table aggregation with 'size' gives an error “'Series' object has no attribute 'columns'”

我只是一个虾纸丫 提交于 2020-05-17 07:04:50
问题 I have the following DataFrame: df = pd.DataFrame({'foo': [1, 3, 3, 4], 'bar': [2, 5, 8, 9], 'abc': [3, 7, 2, 4]}) When I tried using the command: df.pivot_table('bar', 'foo', aggfunc='size') I get the error: AttributeError: 'Series' object has no attribute 'columns' Strangely, the problem disappears when I aggregate several columns simultaneously: df.pivot_table(['bar', 'abc'], 'foo', aggfunc='size') But reappears again if I add the "dropna=False" parameter: df.pivot_table(['bar', 'abc'],

Pandas pivot table aggregation with 'size' gives an error “'Series' object has no attribute 'columns'”

自作多情 提交于 2020-05-17 07:04:27
问题 I have the following DataFrame: df = pd.DataFrame({'foo': [1, 3, 3, 4], 'bar': [2, 5, 8, 9], 'abc': [3, 7, 2, 4]}) When I tried using the command: df.pivot_table('bar', 'foo', aggfunc='size') I get the error: AttributeError: 'Series' object has no attribute 'columns' Strangely, the problem disappears when I aggregate several columns simultaneously: df.pivot_table(['bar', 'abc'], 'foo', aggfunc='size') But reappears again if I add the "dropna=False" parameter: df.pivot_table(['bar', 'abc'],

How to transfer multiple data from one collection to another using $out aggregation

不问归期 提交于 2020-05-17 06:43:10
问题 I am working on nestjs and I have two collections one is order and the second is payment. Now I need to retrieve one document from the collection order and save it into payment collection which is working properly but the issue is when I am trying to save the second document into payment collection then my first document is overwritten. In other words, the first document has vanished after submitting the second document. I want to save every document in payment collection which i retrieved

Filter nested array with conditions based on multi-level object values and update them - MongoDB aggregate + update

二次信任 提交于 2020-05-14 02:27:05
问题 Considering I have the following documents in a collection (ignoring the _id ) : [ { "Id": "OP01", "Sessions": [ { "Id": "Session01", "Conversations": [ { "Id": "Conversation01", "Messages": [ { "Id": "Message01", "Status": "read", "Direction": "inbound" }, { "Id": "Message02", "Status": "delivered", "Direction": "internal" }, { "Id": "Message03", "Status": "delivered", "Direction": "inbound" }, { "Id": "Message04", "Status": "sent", "Direction": "outbound" } ] }, { "Id": "Conversation02",

Why am I getting “unrecognised argument to dateFromString: 'format'” despite having db version >3.6 which supports the aggregation

南笙酒味 提交于 2020-04-16 03:11:11
问题 I have some dates that are string formatted like this in chats: "_id" : ObjectId("2bfd5d45348ef655b5236d93"), "status" : "closed", "context" : "Chats", "chats" : { "time_initialized" : "2019-02-20T17:35:39.960284505Z", "time_responded" : "2019-02-20T17:42:06.691469546Z", "time_closed" : "2019-02-20T17:44:10.158421784Z" } My DB version in mongo is 3.6.5: In the Mongo documentation, you can see below that the function dateFromString is supported: Here's my code that is failing with: "errmsg" :

MongoDB 聚合管道使用

拜拜、爱过 提交于 2020-04-07 16:40:49
数据准备 [ { "name": { "first_name": "qingquan", "last_name": "zeng" }, "balance": 100 }, { "name": { "first_name": "fengxia", "last_name": "yu" }, "balance": 200 } ] 插入数据 db.accounts.insert([{"name": {"first_name": "qingquan","last_name": "zeng"},"balance": 100},{"name": {"first_name": "fengxia","last_name": "yu"},"balance": 200}]) 数据查询 $project # aggregate 中的 $project 除了可以实现投影效果,还直接使用了一个不存在的字段 client_name ,相当于 mysql 中的 as 语法 > db.accounts.aggregate([{ ... $project:{ ... _id:0, ... balance:1, ... client_name:"$name.first_name" ... } ... }]); { "balance" : 100, "client_name" : "qingquan" } {

oracle优化之count的优化-避免全表扫描

老子叫甜甜 提交于 2020-04-07 06:00:00
select count(*) from t1; 这句话比较简单,但很有玄机!对这句话运行的理解,反映了你对数据库的理解深度! 建立实验的大表他t1 SQL> conn scott/tiger 已连接。 SQL> drop table t1 purge; 表已删除。 SQL> create table t1 as select * from emp where 0=9; 表已创建。 SQL> insert into t1 select * from emp; 已创建14行。 SQL> insert into t1 select * from t1; 已创建14行。 SQL> / 已创建28行。 SQL> / 已创建56行。 SQL> / 已创建112行。 SQL> / 已创建224行。 SQL> / 已创建448行。 SQL> / 已创建896行。 SQL> / 已创建1792行。 SQL> / 已创建3584行。 SQL> / 已创建7168行。 SQL> / 已创建14336行。 SQL> / 已创建28672行。 SQL> / 已创建57344行。 SQL> commit; 提交完成。 收集统计信息 SQL> execute dbms_stats.gather_table_stats('SCOTT','T1'); PL/SQL 过程已成功完成。 SQL> SET AUTOT

MongoDB聚合(aggregate)

元气小坏坏 提交于 2020-04-04 06:31:31
https://www.cnblogs.com/wt7018/p/11929359.html MongoDB聚合(aggregate) 一、基础 1、什么是聚合? 聚合是基于数据处理的聚合管道,每个文档通过一个有多个阶段(stage)组成的管道 可以对每个阶段的管道进行分组、过滤等功能,然后经过一系列的处理,输出相应的结果 db.集合名称.aggregate({管道: {表达式}}) 有点像Django中ORM聚合的语法 2、常用管道 $group: 将集合中的文档分组,用于统计结果 $match: 过滤数据,只输出符合条件的文档 $project: 修改输入文档的结构,如重命名、增加、删除字段、创建计算结果 $sort: 将输入文档排序后输出 $limit: 限制聚合管道返回的文档数 $skip: 跳过指定数量的文档,并返回余下的文档 $unwind(): 将列表(数组)类型的字段进行拆分 3、常用表达式 处理输入文档,并输出 语法: 表达式:'$列名' 常用表达式 $sum: 计算总和, $sum:1 表示以一倍计数 $avg: 计数平均值 $min: 获取最小值 $max: 获取最大值 $push: 在结果文档中插入值到一个数组中 $first: 根据资源文档的排序获取第一个文档数据 $last: 根据资源文档的排序获取最后一个文档数据 二、常用管道用法 1、$group

mongodb 常用操作

試著忘記壹切 提交于 2020-04-03 09:07:45
#备份 mongodump.exe -h 127.0.0.1:27017 -d medicine -c jddj -o C:\MongoDB\Server\4.2\backup #恢复 mongorestore.exe -h 127.0.0.1:27017 -d medicine -c jddj C:\MongoDB\Server\4.2\backup\medicine\jddj.bson #导出json、csv数据 mongoexport.exe -h 127.0.0.1:27017 -d medicine -c jddj -o E:\爬虫项目\json\jddj.json mongoexport.exe -h 127.0.0.1:27017 -d medicine -c jddj --type=csv -f 字段1,字段2,字段3 -o E:\爬虫项目\csv\jddj.csv #导入json、csv数据 mongoexport.exe -h 127.0.0.1:27017 -d medicine -c jddj E:\爬虫项目\json\jddj.json mongoimport.exe -h 127.0.0.1:27017 -d medicine -c jddj --type csv --headerline --file E:\爬虫项目\csv\jddj.csv

Need to group records based on matching reversal in sql

拈花ヽ惹草 提交于 2020-03-28 07:04:04
问题 I have a tricky scenario to aggregate the data. Data in my source table is as follows. CustomerId Transaction Type Transaction Amount 1 Payment 100 1 ReversePayment -100 1 payment 100 1 ReversePayment -100 1 Payment 100 1 Payment 100 Requirement is as follows: If the payment as a assoociated Reversepayment with matched amount, sum these two records. If the payment does not have an associated Reverse payment, consider it as orphan(dont sum it). I want output to be like this. CustomerId