pandas包

  # 引入包

  import pandas as pd

  import numpy as np

  import matplotlib.pyplot as plt

  Series

  Series 是一维带标签的数组,数组里可以放任意的数据(整数,浮点数,字符串,Python Object)。其基本的创建函数是:

  s = pd.Series(data, index=index)

  其中 index 是一个列表,用来作为数据的标签。data 可以是不同的数据类型:

  Python 字典

  ndarray 对象

  一个标量值,如 5

  Series创建

  s = pd.Series([1,3,5,np.nan,6,8])

  Series日期创建

  # 生成日期 从2013-01-01 生成至 2013-01-06

  dates = pd.date_range('20130101', periods=6)

  # DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04','2013-01-05', '2013-01-06'], dtype='datetime64[ns]', freq='D')

  Series创建列表

  # 生成2列 第一列: 012345678910 第二列: abbcdabacad

  s = pd.Series(list('abbcdabacad'))

  # 统计不同的列名

  s.unique()

  # 统计列名出现的次数

  s.value_counts()

  # 判断第一列是否在列表中

  s.isin(['a', 'b', 'c'])

  Series索引

  # 两列一列abcde 一列5个随机数

  s = pd.Series(np.random.rand(5), index=list('abcde'))

  # s的列名(第一列),是Index对象

  s.index

  # 添加一行 alpha

  s.index.name = 'alpha'

  # 返回所有第一列为'a'的值

  s['a']

  # 是否有重复的index

  s.index.is_unique

  # 返回不重复index

  s.index.unique()

  # 按index分组,求出每组和

  s.groupby(s.index).sum()

  DataFrame

  DataFrame 是二维带行标签和列标签的数组。可以把 DataFrame 想你成一个 Excel 表格或一个 SQL 数据库的表格,还可以相像成是一个 Series 对象字典。它是 Pandas 里最常用的数据结构。

  DataFrame创建

  df = pd.DataFrame(np.random.randn(4, 6), index=list('ADFH'), columns=['one', 'two', 'three', 'four', 'five', 'six'])

  # 添加index 如果该index没有对应值设为NaN

  df2 = df.reindex(index=list('ABCDEFGH'))

  # 重新设置col(行头)

  df.reindex(columns=['one', 'three', 'five', 'seven'])

  # 把NaN值设为默认的0

  df.reindex(columns=['one', 'three', 'five', 'seven'], fill_value=0)

  # fill method 只对行有效

  df.reindex(columns=['one', 'three', 'five', 'seven'], method='ffill')

  # 重置列index

  df.reindex(index=list('ABCDEFGH'), method='ffill')

  DataFrame操作

  df = pd.DataFrame(np.random.randn(4, 6), index=list('ADFH'), columns=['one', 'two', 'three', 'four', 'five', 'six'])

  # 所有index 为'A' col 为'one'的位置的值设置为100

  df.loc['A']['one'] = 100

  # 舍弃index 为'A'的行

  df.drop('A')

  # 舍弃columns 为 'two' 'four'的列

  df2 = df.drop(['two', 'four'], axis=1)

  # 数据拷贝

  df.iloc[0, 0] = 100

  # 获取index 为'one'的行

  df.loc['one']

  DataFrame计算郑州人流手术多少钱 http://mobile.chnk120.com/

  df = pd.DataFrame(np.arange(12).reshape(4, 3), index=['one', 'two', 'three', 'four'], columns=list('ABC'))

  # 每一列作为一个 Series 作为参数传递给 lambda 函数

  df.apply(lambda x: x.max() - x.min())

  # 每一行作为一个 Series 作为参数传递给 lambda 函数

  df.apply(lambda x: x.max() - x.min(), axis=1)

  # 返回多个值组成的 Series

  def min_max(x):

  return pd.Series([x.min(), x.max()], index=['min', 'max'])

  df.apply(min_max, axis=1)

  # applymap 逐元素计算 每个值保留2位小数

  formater = '{0:.02f}'.format

  df.applymap(formater)

  DataFrame列选择/增加/删除

  df = pd.DataFrame(np.random.randn(6, 4), columns=['one', 'two', 'three', 'four'])

  # 第三列 为 第一列 加上 第二列

  df['three'] = df['one'] + df['two']

  # 添加一个flag列 大于0为True 否则为False

  df['flag'] = df['one'] > 0

  # 删除col为'three'的列

  del df['three']

  # 获取被删的

  four = df.pop('three')

  # 选取col为 five

  df['five'] = 5

  #

  df['one_trunc'] = df['one'][:2]

  # 指定插入位置

  df.insert(1, 'bar', df['one'])

  使用assign() 方法来插入新列

  df = pd.DataFrame(np.random.randint(1, 5, (6, 4)), columns=list('ABCD'))

  # 新列Ratio 值为 df['A'] / df['B']

  df.assign(Ratio = df['A'] / df['B'])

  # 新列AB_Ratio CD_Ratio 值为lambda表达式的值

  df.assign(AB_Ratio = lambda x: x.A / x.B, CD_Ratio = lambda x: x.C - x.D)

  DataFrame排序

  df = pd.DataFrame(np.random.randint(1, 10, (4, 3)), index=list('ABCD'), columns=['one', 'two', 'three'])

  # 按index 为one 排序

  df.sort_values(by='one')

  #

  s.rank()

  DataFrame操作

  DataFrame 在进行数据计算时,会自动按行和列进行数据对齐。最终的计算结果会合并两个 DataFrame。

  df1 = pd.DataFrame(np.random.randn(10, 4), index=list('abcdefghij'), columns=['A', 'B', 'C', 'D'])

  df2 = pd.DataFrame(np.random.randn(7, 3), index=list('cdefghi'), columns=['A', 'B', 'C'])

  df1 + df2

  df1 - df1.iloc[0]

pandas基础:Series与DataFrame操作的更多相关文章

  1. 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  2. 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作

    一.reindex() 方法:重新索引 针对 Series   重新索引指的是根据index参数重新进行排序. 如果传入的索引值在数据里不存在,则不会报错,而是添加缺失值的新行. 不想用缺失值,可以用 ...

  3. pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  4. Python之Pandas中Series、DataFrame

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  5. Python之Pandas中Series、DataFrame实践

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  6. pandas学习series和dataframe基础

    PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...

  7. Pandas中Series和DataFrame的索引

    在对Series对象和DataFrame对象进行索引的时候要明确这么一个概念:是使用下标进行索引,还是使用关键字进行索引.比如list进行索引的时候使用的是下标,而dict索引的时候使用的是关键字. ...

  8. [Python] Pandas 中 Series 和 DataFrame 的用法笔记

    目录 1. Series对象 自定义元素的行标签 使用Series对象定义基于字典创建数据结构 2. DataFrame对象 自定义行标签和列标签 使用DataFrame对象可以基于字典创建数据结构 ...

  9. Python数据分析-Pandas(Series与DataFrame)

    Pandas介绍: pandas是一个强大的Python数据分析的工具包,是基于NumPy构建的. Pandas的主要功能: 1)具备对其功能的数据结构DataFrame.Series 2)集成时间序 ...

随机推荐

  1. 201871010132--张潇潇--《面向对象程序设计(Java)》第十一周学习总结

    博文正文开头格式:(2分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.co ...

  2. Premiere Pro cc 2019 下载链接

    [安装环境]:win7/win8/win10 [64位下载] 百度网盘链接:pan.baidu.com/s/1mUDK5-CLafUDXn2Rsl-Mjw   提取码:ecx4

  3. Python进阶-VII 内置函数

    一.内置函数引入 我们已经了解的有; print()  input() range() next()  dir() str() int() list() set() tuple() dict() he ...

  4. Excel-统计函数

    1.Count系列函数 COUNT 数字个数----下面结果为 4 counta 非空的字数 ----下面为6 COUNTBLANK ------非空个数  ---- 下面为9 如何将字符串形式的数字 ...

  5. 洛谷P3232[HNOI2013]游走

    有一个无向简单连通图,顶点从 \(1\) 编号到 \(n\),边从 \(1\) 编号到 \(m\) 小Z在该图上进行随机游走,初始时小Z在\(1\)号顶点,每一步小Z以相等的概率随机选 择当前顶点的某 ...

  6. [LeetCode] 738. Monotone Increasing Digits 单调递增数字

    Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...

  7. Matlab:Toeplitz矩阵-向量乘法的快速傅里叶(FFT)算法

    一.$\tt Toeplitz$矩阵与循环($\tt Circulant$)矩阵 定义 为$n\times n$阶循环矩阵. 定义 $T_n(i,j)=t_{j-i} $  为$n\times n$ ...

  8. docker下oracle的awr报告下载

  9. SqlServer 通过日志恢复数据库

    前期工作 查看数据属性,确保下条件: 1.数据库属性->选项->恢复模式=完整 2.建好库以后.一个数据库完整的数据备份 3.到出事期间日志没有你间断 4.记录出事的准确时间 一.数据准备 ...

  10. python 多线程剖析

    先来看个栗子: 下面来看一下I/O秘籍型的线程,举个栗子——爬虫,下面是爬下来的图片用4个线程去写文件 #!/usr/bin/env python # -*- coding:utf-8 -*- imp ...