Python pandas DateFrame查询数据df.loc的5种方法

# _*_coding:utf-8_*_
import pandas as pd# 自定义函数
def func(df):return df['temperature'] < 32# Press the green button in the gutter to run the script.
if __name__ == '__main__':data = {'date': ['2022-08-21', '2022-08-22', '2022-08-23'],'weather': ["晴", "阴", "晴"],'temperature': ["30℃", "20℃", "32℃"]}df = pd.DataFrame(data)print(df)# 1.使用单个标签值查询# 查询出"日期"列的数据print(df["date"])# 2.使用标签值列表查询# 查询出"日期"和"温度"列的数据print(df[['date', 'weather']])# 3.使用数值区间查询# 查询出一行数据print(df.loc[1])# 查询出多行数据(包含末尾元素,和切片不同)print(df.loc[1:2])# ----------------------------------------------------# 设置索引为日期,方便按日期筛选df.set_index('date', inplace=True)print(df)# 1.使用单个标签值查询print(df.loc['2022-08-21', 'temperature'])# 2.使用值列表查询print(df.loc[['2022-08-21', '2022-08-22'], 'weather'])# 3.使用数值区间查询df.loc[:, 'temperature'] = df['temperature'].str.replace('℃', '').astype(int)print(df.loc[:, 'weather':'temperature'])# 4.使用条件表达式查询(bool列表的长度得等于行数或者列数)print(df.loc[df['temperature'] >= 30, :])# 5.使用函数查询# 直接使用lambda表达式print(df.loc[lambda df1: (df1['temperature'] >= 30) & (df1['temperature'] < 32), :])# 自定义函数print(df.loc[func, :])

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546270.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

想读Spring源码?先从这篇「 极简教程」开始吧...

为什么要阅读源码&#xff1f;这是一个有趣的问题&#xff0c;类似的问题还有&#xff0c;为什么要看书&#xff1f;为什么要爬山&#xff1f; 这也是一个哲学问题&#xff0c;我想每个人都有不同的答案&#xff0c;下面我是对阅读源码好处的一些思考。 &#xff08;PS&#x…

Python pandas DateFrame新增数据列

下面代码在Linux下运行 # _*_coding:utf-8_*_ import os import pandas as pd# 自定义函数 def func(x):if x[temperature] > 30:return 高温if x[temperature] < 10:return 低温return 常温# Press the green button in the gutter to run the script. if __name__ __…

python函数示例_带Python示例的float()函数

python函数示例Python float()函数 (Python float() function) float() function is a library function in Python, it is used to get a float value from a given number or a string. It accepts either a string (that should contain a number) or a number and returns …

ALP规则的验证

实验1&#xff1a;ALP规则的验证实验目标创建本地用户jack、tom、mike,创建本地组group1&#xff0c;并把所建的用户加入到group1中&#xff0c;通过ALP规则实现以上用户对e:\share\1.txt 文件内容读取和写入权限实验环境略实验步骤一、 创建本地用户jack、tom、mike右击计算机…

6大分布式定时任务对比

作者 | sharedCode来源 | blog.csdn.net/u012394095/article/details/79470904分布式定时任务简介 把分散的&#xff0c;可靠性差的计划任务纳入统一的平台&#xff0c;并实现集群管理调度和分布式部署的一种定时任务的管理方式&#xff0c;叫做分布式定时任务。常见开源方案 e…

Python利用pandas获取每行最大值和最小值

知识点 1.找出每一行最大值和对应的列索引&#xff1a; #找出每行最大值对应的索引 df[max_idx]df.idxmax(axis1) #取出该最大值 df[max_val]df.max(axis1)2.找出每一行最小值和对应的列索引&#xff1a; # 找出每行最小值对应的索引 df[min_idx]df.idxmin(axis1) # 取出该最…

一文搞懂 ThreadLocal 原理

当多线程访问共享可变数据时&#xff0c;涉及到线程间同步的问题&#xff0c;并不是所有时候&#xff0c;都要用到共享数据&#xff0c;所以就需要线程封闭出场了。数据都被封闭在各自的线程之中&#xff0c;就不需要同步&#xff0c;这种通过将数据封闭在线程中而避免使用同步…

python中assert_在Python中带有示例的assert关键字

python中assertPython断言关键字 (Python assert keyword) assert is a keyword (case-sensitive) in python, it is used to debug the code. Generally, it can be used to test a condition – if a condition returns False – it returns an Assertion Error (AssertionEr…

单域MPLS ***数据转发实验分析

MPLS 数据详细转发流程示意图&#xff1a;配置思路&#xff1a;在自治系统AS100中配置IGP&#xff0c;使得两台PE路由器的lo 0接口路由可达&#xff1b;两台PE路由器路由可达后&#xff0c;在两台PE路由器之间建立MP-iBGP邻居关系&#xff0c;用来传输V4路由&#xff1b;AS100中…

python中list函数_list()函数以及Python中的示例

python中list函数Python list()函数 (Python list() function) list() function is a library function, it is used to create a list, it accepts multiple elements enclosed within brackets (because list() takes only one argument. Thus, the set of elements within b…

IDEA 终于支持中文版和 JDK 直接下载了(太方便了)附新版介绍视频

这是我的第 48 篇原创文章。IDEA 2020.1 经过了漫长的打磨终于发布正式版了&#xff0c;而这次的版本不止直接支持 Java 14&#xff0c;还带来了两个重量级的功能&#xff1a;官方中文版支持和 JDK 直接下载。在之前的开发中&#xff0c;当我们需要下载 JDK 时&#xff0c;通常…

【BO】WEBI文件打开时提示Illegal access错误

在infoview中打开WEBI文件时&#xff0c;提示如下错误。 通过查看SCN&#xff0c;找到错误原因是CMC中有一个服务没有启动。 启动这个服务即可&#xff1a; WebIntelligenceProcessingServer转载于:https://www.cnblogs.com/uzipi/p/3905513.html

华为交换机链路聚合使用ENSP模拟器进行实验

一、简介 链路聚合(Eth-Trunk)是将多个物理接口捆绑为一个逻辑接口,实现增加链路带宽,提高可靠性,提供负载分担的目的。 二、华为ENSP模拟器仿真图 华为交换机LSW1和LSW2端口G0/0/2、G0/0/3实现链路聚合,这2个交换机通过静态路由实现2个不同网段V10、V20互通。 三、华…

为什么工作很卖力,最后还晋升不了?

作者 | 军哥来源 | 军哥手记&#xff08;公众号ID&#xff1a;zxhy_cj&#xff09;最近写文章多了一些思考&#xff0c;要写对大家有价值的、有启发、有思考的东西&#xff0c;希望读者朋友们enjoy&#xff01;为什么自我感觉良好&#xff0c;可是晋升没我&#xff1f;感觉老板…

python 示例_Python中带有示例的class关键字

python 示例Python类关键字 (Python class keyword) class is a keyword (case-sensitive) in python, it is used to define a class, when we need to define a class, before the class name - we must have to use class keyword. class是python中的一个关键字(区分大小写)…

基于Java的数据采集(终结篇)

关于写过关于JAVA采集入库的三篇文章&#xff1a; 基于Java数据采集入库&#xff08;一&#xff09;&#xff1a;http://www.cnblogs.com/lichenwei/p/3904715.html 基于Java数据采集入库&#xff08;二&#xff09;&#xff1a;http://www.cnblogs.com/lichenwei/p/3905370.ht…

华为交换机、路由器配置单臂路由实现不同网段通信

一、eNSP模拟器仿真图 二、华为交换机配置 //批量创建VLAN [Huawei]vlan batch 10 20 //PC1电脑与交换机连接端口配置 [Huawei]interface GigabitEthernet0/0/2 [Huawei-GigabitEthernet0/0/2]port link-type access [Huawei-GigabitEthernet0/0/2]port default vlan 10 [Huaw…

python中的Lambda表达式/函数

Explanation: 说明&#xff1a; In python, there is a function named Lambda. Lambda function is an anonymous function - that means the function which does not have any name. 在python中&#xff0c;有一个名为Lambda的函数。 Lambda函数是一个匿名函数-表示该函数…

IDEA 正式版终于支持中文版和 JDK 直接下载了(太方便了)附介绍视频

IDEA 2020.1 经过了漫长的打磨终于发布正式版了&#xff0c;而这次的版本不止直接支持 Java 14&#xff0c;还带来了两个重量级的功能&#xff0c;官方中文版支持和 JDK 直接下载。 在之前的开发中&#xff0c;当我们需要下载 JDK 时&#xff0c;通常的步骤是这样的&#xff1…

HoughLine变换

对于HoughLine变换&#xff0c;有两种方法&#xff0c;标准霍夫变换&#xff08;SHT&#xff09;用的矩阵是CV_32FC2&#xff0c;用极坐标法记录直线,而累积概率霍夫变换&#xff08;PPHT&#xff09;用的是CV_32FC核心函数&#xff1a;cvCvtColor&#xff0c;cvHoughLines2&am…