pandas 基础

serise

import pandas as pd
from pandas import Series, DataFrame
obj = Series([4, -7, 5, 3])
obj
0    4
1 -7
2 5
3 3
dtype: int64
obj.values
array([ 4, -7,  5,  3], dtype=int64)
obj.index
RangeIndex(start=0, stop=4, step=1)
obj[[1,3]]
# 跳着选取数据
1   -7
3 3
dtype: int64
obj[1:3]
1   -7
2 5
dtype: int64
pd.isnull(obj)
0    False
1 False
2 False
3 False
dtype: bool
  • reindex可以用来插值
obj.reindex(range(5), method = 'ffill')
0    4
1 -7
2 5
3 3
4 3
dtype: int64
  • 标签切片是闭区间的

dataframe

data = {'state': ['asd','qwe','sdf','ert'],
'year': [2000, 2001, 2002, 2003],
'pop': [1.5,1.7,3.6,2.4]}
data = DataFrame(data)
data

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
pop state year
0 1.5 asd 2000
1 1.7 qwe 2001
2 3.6 sdf 2002
3 2.4 ert 2003
data.year
# 比r里提取列要方便点
0    2000
1 2001
2 2002
3 2003
Name: year, dtype: int64
data['debt'] = range(4)
data

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
pop state year debt
0 1.5 asd 2000 0
1 1.7 qwe 2001 1
2 3.6 sdf 2002 2
3 2.4 ert 2003 3
  • index是不能修改的
a = data.index
a[1] = 6
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-9-57677294f950> in <module>()
1 a = data.index
----> 2 a[1] = 6 F:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in __setitem__(self, key, value)
1668
1669 def __setitem__(self, key, value):
-> 1670 raise TypeError("Index does not support mutable operations")
1671
1672 def __getitem__(self, key): TypeError: Index does not support mutable operations
data.columns
Index(['pop', 'state', 'year', 'debt'], dtype='object')
  • .ix标签索引功能,输入行和列
  • 不加.ix只能选取其中的某列或某行,不能列与行同时选取
data[:3]

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
pop state year debt
0 1.5 asd 2000 0
1 1.7 qwe 2001 1
2 3.6 sdf 2002 2
data.ix[:,:3]

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
pop state year
0 1.5 asd 2000
1 1.7 qwe 2001
2 3.6 sdf 2002
3 2.4 ert 2003
  • 删除某列用drop,axis = 0表示行,1表示列
  • 删除后原数据不变
data.drop(0,axis=0)

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
pop state year debt
1 1.7 qwe 2001 1
2 3.6 sdf 2002 2
3 2.4 ert 2003 3
data.drop('year', axis=1)

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
pop state debt
0 1.5 asd 0
1 1.7 qwe 1
2 3.6 sdf 2
3 2.4 ert 3
data

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
pop state year debt
0 1.5 asd 2000 0
1 1.7 qwe 2001 1
2 3.6 sdf 2002 2
3 2.4 ert 2003 3
import numpy as np
df = DataFrame(np.arange(9).reshape(3, 3))
df

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
0 1 2
0 0 1 2
1 3 4 5
2 6 7 8
  • applymap()可以对dataframe每一个元素运用函数
  • apply()可以对每一维数组运用函数
df.applymap(lambda x: '%.2f' % x)

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
0 1 2
0 0.00 1.00 2.00
1 3.00 4.00 5.00
2 6.00 7.00 8.00
data.sort_values(by='pop')
# 对某一列排序

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
pop state year debt
0 1.5 asd 2000 0
1 1.7 qwe 2001 1
3 2.4 ert 2003 3
2 3.6 sdf 2002 2
data.describe()

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
pop year debt
count 4.000000 4.000000 4.000000
mean 2.300000 2001.500000 1.500000
std 0.948683 1.290994 1.290994
min 1.500000 2000.000000 0.000000
25% 1.650000 2000.750000 0.750000
50% 2.050000 2001.500000 1.500000
75% 2.700000 2002.250000 2.250000
max 3.600000 2003.000000 3.000000
df.isin([1])

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
0 1 2
0 False True False
1 False False False
2 False False False
  • None、NaN会被当作NA处理
  • df.shape不加括号相当于dim()
df.shape
(3, 3)
  • dropna删除缺失值
df.ix[:1, :1] = None
df

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
0 1 2
0 NaN NaN 2
1 NaN NaN 5
2 6.0 7.0 8
  • 填充缺失值可以调用字典,不同行添加不同值
df.fillna({0:11, 1:22})

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
0 1 2
0 11.0 22.0 2
1 11.0 22.0 5
2 6.0 7.0 8
df

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
0 1 2
0 NaN NaN 2
1 NaN NaN 5
2 6.0 7.0 8
df.fillna({0:11, 1:22}, inplace=True)

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
0 1 2
0 11.0 22.0 2
1 11.0 22.0 5
2 6.0 7.0 8
df

.dataframe thead tr:only-child th {
text-align: right;
}

.dataframe thead th {
text-align: left;
} .dataframe tbody tr th {
vertical-align: top;
}
0 1 2
0 11.0 22.0 2
1 11.0 22.0 5
2 6.0 7.0 8
  • inplace修改对象不产生副本

python学习笔记(四):pandas基础的更多相关文章

  1. python学习笔记四 迭代器,生成器,装饰器(基础篇)

    迭代器 __iter__方法返回一个迭代器,它是具有__next__方法的对象.在调用__next__方法时,迭代器会返回它的下一个值,若__next__方法调用迭代器 没有值返回,就会引发一个Sto ...

  2. Python学习笔记一(基础信息)

    目录 输入输出 数据类型和变量 整数 浮点数 字符串 布尔值 空值 变量 常量 小结 欢迎关注我的博客我在马路边 说明:此笔记不是从零开始,在学习的过程中感觉需要记录一些比较重要和需要重复浏览的信息, ...

  3. PYTHON 爬虫笔记四:正则表达式基础用法

    知识点一:正则表达式详解及其基本使用方法 什么是正则表达式 正则表达式对子符串操作的一种逻辑公式,就是事先定义好的一些特定字符.及这些特定字符的组合,组成一个‘规则字符串’,这个‘规则字符串’用来表达 ...

  4. 吴裕雄--python学习笔记:爬虫基础

    一.什么是爬虫 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Python爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.URL管理器.网页下载器.网 ...

  5. Python学习笔记(四)Python函数的参数

    Python的函数除了正常使用的必选参数外,还可以使用默认参数.可变参数和关键字参数. 默认参数 基本使用 默认参数就是可以给特定的参数设置一个默认值,调用函数时,有默认值得参数可以不进行赋值,如: ...

  6. Java基础学习笔记四 Java基础语法

    数组 数组的需求 现在需要统计某公司员工的工资情况,例如计算平均工资.最高工资等.假设该公司有50名员工,用前面所学的知识完成,那么程序首先需要声明50个变量来分别记住每位员工的工资,这样做会显得很麻 ...

  7. Python学习笔记四

    一.装饰器 1.知识储备 函数对象 函数可以被引用 函数可以当参数传递 返回值可以是函数 可以当作容器的元素 def func1(): print (666) def func2(): print ( ...

  8. Python学习笔记四:面向对象编程

    一:定义类并创建实例 Python中定义类,通过class关键字,类名开头大写,参数列表为所继承的父类.如果没有需要明确继承的类,则继承object. 使用类来创建对象,只需 类名+() 形式即可,p ...

  9. python学习笔记(四) 思考和准备

    一.zip的坑 zip()函数接收多个可迭代数列,将数列中的元素重新组合,在3.0中返回迭代器指向 数列首地址,在3.0以下版本返回List类型的列表数列.我用的是3.5版本python, 所以zip ...

  10. 【Python学习笔记】Pandas库之DataFrame

    1 简介 DataFrame是Python中Pandas库中的一种数据结构,它类似excel,是一种二维表. 或许说它可能有点像matlab的矩阵,但是matlab的矩阵只能放数值型值(当然matla ...

随机推荐

  1. Java 字符流与基本IO

    字符流基类 java.io包中专门用于字符流处理的类,是以 Reader 和 Writer 为基础派生的一系列类.字符流以字符为单位,根据码表映射字符,一次可能读多个字节,只能处理字符类型的数据.Re ...

  2. lvs安装文档

    安装lvs应用模块 1.安装依赖包: [root@client lvs]# yum -y install ipvs* 2.验证本机ip_vs模块是否加载 [root@client lvs]# -.el ...

  3. java接口实例

    1.开发系统时,主体架构使用接口,接口构成系统的骨架2.这样就可以通过更换接口的实现类来更换系统的实现 public class printerDemo{ public static void mai ...

  4. Google Chrome保存插件方法

    1.拷贝下面地址到记事本 https://clients2.google.com/service/update2/crx?response=redirect&x=id%3D~~~~%26uc ...

  5. jQueryeasyUI+Hibernate+struts2实现商城后台管理之复合类别

    一.在Category.java中添加父类别和子类别两个变量,并生成get/set方法

  6. 基于session和cookie的登录验证(CBV模式)

    基于session和cookie的登录验证(CBV模式) urls.py """cookie_session URL Configuration The `urlpatt ...

  7. Django的模版引擎与模版使用

    Django的模版引擎与模版使用 模版引擎是模版响应的后端.模版指的是HTML.css,js等相关的文件.模版引擎是将这些表示层文件与数据相整合在一起,然后将整合后的数据给到响应类型判断采用一次性响应 ...

  8. CS20Chapter2

    constants操作 import tensorflow as tf a = tf.constant([2, 2], name='a') b = tf.constant([[0, 1], [2, 3 ...

  9. Session["userName"]与Session["userName"].ToString()是不一样的~~

    今天调了个代码,发现老是跟预期的结果不一样,找了半天的原因,一个一个的往回找,终于逮出元凶了! 我今天才发现,下面两个是不一样的: //有问题的代码 if (Session["uid&quo ...

  10. 【题解】洛谷P1073 [NOIP2009TG] 最优贸易(SPFA+分层图)

    次元传送门:洛谷P1073 思路 一开始看题目嗅出了强连通分量的气息 但是嫌长没打 听机房做过的dalao说可以用分层图 从来没用过 就参考题解了解一下 因为每个城市可以走好几次 所以说我们可以在图上 ...