[Python Cookbook] Pandas: Indexing of DataFrame

Selecting a Row

df.loc[index] # if index is a string, add ‘ ‘; if index is a number, no ‘ ‘

or

df.iloc[row_num]

Selecting a Column

df[‘col_name‘]

Or

df.col_name

Selecting an Element

df.loc[index, ‘col_name‘]

Selecting Multiple Discontinuous Rows

df.loc[[row_num1, row_num2, ...]] # can‘t use df.iloc here

Or

df.loc[bool_expr]

E.g.

nyc[nyc.cand_nm.isin(df11 p.cand nm)]
nyc[nyc.cand_nm.isnull()]
nyc[nyc.cand_nm.isnull()]
nyc[nyc.contb_receipt_amt >0]

原文地址:https://www.cnblogs.com/sherrydatascience/p/10360730.html

时间: 2024-08-06 03:13:13

[Python Cookbook] Pandas: Indexing of DataFrame的相关文章

python数据分析pandas中的DataFrame数据清洗

pandas中的DataFrame中的空数据处理方法: 方法一:直接删除 1.查看行或列是否有空格(以下的df为DataFrame类型,axis=0,代表列,axis=1代表行,以下的返回值都是行或列索引加上布尔值)• isnull方法 • 查看行:df.isnull().any(axis=1)  • 查看列:df.isnull().any(axis=0)• notnull方法:• 查看行:df.notnull().all(axis=1)• 查看列:df.notnull().all(axis=0

[Python Cookbook] Pandas: 3 Ways to define a DataFrame

Using Series (Row-Wise) import pandas as pd purchase_1 = pd.Series({'Name': 'Chris', 'Item Purchased': 'Dog Food', 'Cost': 22.50}) purchase_2 = pd.Series({'Name': 'Kevyn', 'Item Purchased': 'Kitty Litter', 'Cost': 2.50}) purchase_3 = pd.Series({'Name

[Python Cookbook] Pandas Groupby

Groupby Count # Party's Frequency of donations nyc.groupby('Party')['contb receipt amt'].count() The command returns a series where the index is the name of a Party and the value is the count of that Party. Note that the series is ordered by the name

Python的Pandas库简述

pandas 是 python 的数据分析处理库import pandas as pd 1.读取CSV.TXT文件 foodinfo = pd.read_csv("pandas_study.csv", encoding = "utf-8") 2.查看前N条.后N条信息 foodinfo.head(N) foodinfo.tail(N) 3.查看数据框的格式,是DataFrame还是ndarray print(type(foodinfo)) # 结果:<clas

Python Numpy,Pandas笔记

Numpy Numpy是python的一个库.支持维度数组与矩阵计算并提供大量的数学函数库. #浮点数转int arr = np.array([1.2,1.3,1.4],[1.5,1.6,1.7])#创建ndarray时候也可以指定dtype arr.astype(dtype = np.int) #对数组批量运算,作用在每个元素上 arr = np.array([1,2,3],[4,5,6]) print arr**5 #索引和切片 arr = np.array([1,2,3,4,5,6]) p

python 使用pandas,完成对excel的操作: 遍历,求偏度(skew)的小程序

excel有针对偏度的计算函数 skew(), 但是不清楚怎么使用excel进行遍历, 数据量很大. 尝试使用python进行解决. 第一次学习python,没想到了在克服安装各种包的难过之后,居然成功实现了. python3.3: #this is a test case # -*- coding: gbk -*- print("hello python!中文") #env config import xlrd import os import xlwt3 import numpy

python之pandas简单介绍及使用(一)

一. Pandas简介 1.Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.pandas提供了大量能使我们快速便捷地处理数据的函数和方法.你很快就会发现,它是使Python成为强大而高效的数据分析环境的重要因素之一. 2.Pandas 是python的一个数据分析包,最初由AQR Capital Management

Python数据分析--Pandas知识点(二)

本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘. Python数据分析--Pandas知识点(一) 下面将是在知识点一的基础上继续总结. 13. 简单计算 新建一个数据表df 1 import pandas as pd 2 3 df = pd.DataFrame({"地区": ["A区","B区", "C区"], 4 "前半年销量": [3500, 4500,3800], 5

Python数据分析--Pandas知识点(三)

本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘. Python数据分析--Pandas知识点(一) Python数据分析--Pandas知识点(二) 下面将是在知识点一, 二的基础上继续总结. 前面所介绍的都是以表格的形式中展现数据, 下面将介绍Pandas与Matplotlib配合绘制出折线图, 散点图, 饼图, 柱形图, 直方图等五大基本图形. Matplotlib是python中的一个2D图形库, 它能以各种硬拷贝的格式和跨平台的交互式环境生成高质量的图形,