Python中使用len函数和nunique函数计算多个Dataframe分组的数量

Python中使用len函数和nunique函数计算多个Dataframe分组的数量

在Python数据分析中,我们通常需要对数据进行聚合,并得到不同分组的数量。在此过程中,我们可以使用多种方法对数据进行操作,包括使用len()函数和nunique()函数。

在本篇文章中,我们将介绍如何使用这两个函数来计算多个Dataframe分组的数量。

首先,我们需要借助Pandas库处理数据。利用groupby()方法可以方便地对数据进行分组操作。同时,我们需要生成多个Dataframe实例,以便进行多级分组操作。

假设我们有三个Dataframe,分别为df1, df2和df3,我们需要对它们进行分组操作。

代码如下:

import pandas as pd
import numpy as np

# 生成数据
data1 = {
   
   'key': 
Pandas GroupBy深度解析:从分组聚合到数据思维引擎 GroupBy是Pandas中实现数据分组聚合的核心机制,其本质并非即时计算函数,而是基于延迟求值的逻辑视图构造器。理解其底层原理(如MultiIndex索引结构、agg函数组合策略、apply性能边界)对构建可复用、可验证的数据分析流水线至关重要。它支撑着销售看板、客户分层、时间趋势等典型业务场景,广泛应用于电商、金融、物联网等领域的特征工程与报表自动化。掌握GroupBy方法与pandas分组聚合的工程实践,能显著提升数据处理效率与结果可信度,避免常见KeyError、广播错误及性能陷阱。 阅读详情

相关推荐

pandas中安全删除DataFrame列的7种实战场景与规范

在数据清洗中,删除DataFrame列并非简单的语法操作,而是涉及数据结构一致性、内存管理与协作可追溯性的关键环节。其底层原理依赖pandas的BlockManager机制和axis维度语义控制,技术价值在于保障后续分析链路(如groupby、模型训练、可视化)的稳定性与可复现性。典型应用场景包括处理全空列、常量列、多级索引层级、正则匹配列名、合规字段剔除等。本文聚焦pandas drop方法的核心参数(axis='columns'、inplace=False、errors='ignore')与工程实践闭环

weixin_30800807的博客 324

Pandas DataFrame实战指南:真实业务数据清洗与分析全流程

DataFrame是Pandas中用于结构化数据操作的核心容器,其本质是支持向量化运算、类型感知与链式调用的内存数据结构。理解其底层基于NumPy数组的dtype机制,是解决性能瓶颈(如to_datetime慢、groupby卡顿)和类型错误(如字符串混入数值列)的关键。技术价值在于将原始异构数据(CSV/Excel/JSON)快速转化为可验证、可复用、可配置的分析就绪状态。典型应用场景包括电商订单清洗、IoT时序对齐、BI看板底表生成等需兼顾正确性、效率与可维护性的生产任务。本文聚焦真实项目中的加载、探查

weixin_34200628的博客 326

pandas DataFrame内存大小查询与优化实战指南

DataFrame大小不仅是shape维度的行列表达,更是关乎实际内存占用的核心性能指标。其底层涉及NumPy dtype对齐、object类型堆内存分配、category压缩机制及Arrow-backed数组等关键技术原理。准确评估内存开销可避免MemoryError、提升ETL效率、降低云资源成本,并支撑中大型数据(>10万行或含文本/时间列)的稳定处理。本文聚焦df.memory_usage(deep=True)这一黄金标准,结合dtype优化、列级诊断、读取参数调优与跨版本兼容性实践,提供从加载、清

weixin_33801856的博客 335

pandas DataFrame内存占用精准评估与优化指南

DataFrame大小不仅关乎行数列数,更取决于数据类型、内存布局与对象引用机制。理解pandas底层如何存储int64、float64、object及category等dtype,是准确评估内存消耗的前提;`memory_usage(deep=True)`可穿透计算字符串等内容真实开销,而`dtypes`优化(如用category替代object)能实现3–5倍内存压缩;该技术广泛应用于金融风控、电商日志、IoT时序分析等大数据场景,尤其在处理CSV加载后OOM频发、Jupyter卡顿、生产环境内存泄漏等

weixin_30258901的博客 355

Pandas read_excel()函数全解析:从Excel数据导入到DataFrame处理实战

在Python数据分析与自动化办公领域,数据导入是数据处理流程的首要环节。Pandas库作为Python数据分析的核心工具,其read_excel()函数提供了将Excel文件高效转换为DataFrame数据结构的标准化接口。该函数通过整合openpyxl、xlrd等底层引擎,实现了对.xlsx和.xls格式文件的统一读取,支持复杂表头、数据类型指定、分块读取等高级功能,显著提升了数据处理的自动化程度与准确性。在数据清洗、报表自动化、机器学习预处理等应用场景中,掌握read_excel()的参数配置与性能优

weixin_34138056的博客 342

pandas使用len函数和max函数计算dataframe中指定字符串数据列中字符串的最大长度(length of the longest string in a column)

pandas使用len函数和max函数计算dataframe中指定字符串数据列中字符串的最大长度(length of the longest string in a column)

data+scenario+science+insight 5612

Xilinx PL端DP显示接口开发实战(一)——环境搭建与硬件选型

本文详细介绍了Xilinx PL端DP显示接口开发的环境搭建与硬件选型实战经验。从Vivado和Vitis 2021.1版本的软件安装要点,到DP141和MCDP6000芯片的选型建议,再到DP1.4接口的RX/TX方向开发关键点,为开发者提供了全面的技术指导。文章还分享了IP核配置、BD原理图设计以及常见问题排查的实用技巧,帮助开发者高效完成Xilinx DP接口开发项目。

weixin_29250573的博客 179

基于STM32的多路火灾报警系统设计

本文设计了一款基于STM32的多路火灾报警系统,利用多个传感器组合(如烟雾传感器、温度传感器等)进行数据采集与处理,精确判断火灾情况,并及时发出报警信号,保障生命与财产安全。本系统设计了一款基于STM32的多路火灾报警系统,通过结合烟雾传感器和温度传感器,提升了火灾检测的准确性和可靠性。未来,系统可以进一步集成更多传感器,如气体传感器、湿度传感器等,进一步提升火灾报警系统的智能化水平。本系统采用STM32F103单片机作为核心处理单元,结合烟雾传感器、温度传感器和报警模块,实现多通道火灾报警功能。

stm32d1219的博客 2036

4G+GNSS定位模块DX-CT511N-B:AT指令实战与多协议通信指南

本文详细介绍了4G+GNSS定位模块DX-CT511N-B的AT指令实战应用与多协议通信配置。内容涵盖TCP/UDP通信、MQTT协议集成、HTTP服务交互以及GNSS定位功能的深度解析,为开发者提供从基础指令到高级调试的完整指南,助力物联网设备开发。

hope8的博客 294

QT编程之OSG

OSG(OpenSceneGraph)与Qt的集成为开发3D可视化应用提供了一个强大的平台。Qt提供了图形用户界面(GUI)工具,而OSG负责高效的3D渲染和场景管理。通过将OSG嵌入Qt应用程序中,开发者可以实现丰富的用户界面与复杂的3D渲染效果的结合。

深之JohnChen的专栏 1610

【Unity实战篇 】| 游戏中实现镂空遮罩效果【矩形、圆形镂空遮罩】

本文来写一下怎样在Unity中完成一个 镂空遮罩 的效果。 镂空遮罩 比较常用的有两种:矩形镂空遮罩、圆形镂空遮罩。 镂空遮罩 在游戏中常被当做新手引导必备的内容,可以给玩家更多不一样的体验。 下面就一起来看一下怎样做出这种效果吧!

努力前行,总会成为自己心中的那道光 1万+

Django 6.0:有哪些新特性以及它们对实际项目的重要性

Django 6.0 感觉像是一个成熟的版本,它基于实际应用案例而打造。它减少了不必要的复杂性,同时扩展了框架的开箱即用功能。升级到 Django 6.0 不仅仅是为了保持版本更新,更是为了构建更易于维护、更安全、更能适应未来发展的应用程序。

iCloudEnd的博客 348

【文献笔记】CVPR 2023|Attention-Based Point Cloud Edge Sampling

优点能够有效采出点云数据的边缘点分类任务性能优越不足没有设计出一个更好的上采样方法,导致分割任务的性能略低只考虑了边缘特征,没有利用其他几何特征。

m0_55908255的博客 324

大模型——用LM Studio本地运行大模型

LM Studio 是一款流行的 GUI 应用程序,它允许具有基本计算机知识的用户在其 Linux 机器上轻松下载、安装和运行大型语言模型 (LLM)。LM Studio 是一款流行的 GUI 应用程序,它允许具有基本计算机知识的用户在其 Linux 机器上轻松下载、安装和运行大型语言模型 (LLM)。可以使用 LM Studio 聊天界面轻松安装、设置和访问流行的 LLM 模型,例如 Llama 3、Phi3、Falcon、Mistral、StarCoder、Gemma 等。

3054

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

上一篇: 深度学习技术正在飞速发展,越来越多的人开始关注和应用它们
下一篇: Python编程实现平均绝对偏差算法
普通网友
博客等级 码龄4年 1011粉丝 1742原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编码实践

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址