python学习笔记:pandas基础
Posted 嘻呵呵
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习笔记:pandas基础相关的知识,希望对你有一定的参考价值。
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
array([ 4, -7, 5, 3], dtype=int64)
RangeIndex(start=0, stop=4, step=1)
1 -7
3 3
dtype: int64
1 -7
2 5
dtype: int64
0 False
1 False
2 False
3 False
dtype: bool
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
| pop | state | year |
0 | 1.5 | asd | 2000 |
1 | 1.7 | qwe | 2001 |
2 | 3.6 | sdf | 2002 |
3 | 2.4 | ert | 2003 |
0 2000
1 2001
2 2002
3 2003
Name: year, dtype: int64
data['debt'] = range(4)
data
| 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 |
---------------------------------------------------------------------------
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
Index(['pop', 'state', 'year', 'debt'], dtype='object')
- .ix标签索引功能,输入行和列
- 不加.ix只能选取其中的某列或某行,不能列与行同时选取
| pop | state | year | debt |
0 | 1.5 | asd | 2000 | 0 |
1 | 1.7 | qwe | 2001 | 1 |
2 | 3.6 | sdf | 2002 | 2 |
| 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表示列
- 删除后原数据不变
| 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)
| pop | state | debt |
0 | 1.5 | asd | 0 |
1 | 1.7 | qwe | 1 |
2 | 3.6 | sdf | 2 |
3 | 2.4 | ert | 3 |
| 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
| 0 | 1 | 2 |
0 | 0 | 1 | 2 |
1 | 3 | 4 | 5 |
2 | 6 | 7 | 8 |
- applymap()可以对dataframe每一个元素运用函数
- apply()可以对每一维数组运用函数
df.applymap(lambda x: '%.2f' % x)
| 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')
# 对某一列排序
| 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 |
| 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 |
| 0 | 1 | 2 |
0 | False | True | False |
1 | False | False | False |
2 | False | False | False |
- None、NaN会被当作NA处理
- df.shape不加括号相当于dim()
(3, 3)
| 0 | 1 | 2 |
0 | NaN | NaN | 2 |
1 | NaN | NaN | 5 |
2 | 6.0 | 7.0 | 8 |
| 0 | 1 | 2 |
0 | 11.0 | 22.0 | 2 |
1 | 11.0 | 22.0 | 5 |
2 | 6.0 | 7.0 | 8 |
| 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)
| 0 | 1 | 2 |
0 | 11.0 | 22.0 | 2 |
1 | 11.0 | 22.0 | 5 |
2 | 6.0 | 7.0 | 8 |
| 0 | 1 | 2 |
0 | 11.0 | 22.0 | 2 |
1 | 11.0 | 22.0 | 5 |
2 | 6.0 | 7.0 | 8 |
以上是关于python学习笔记:pandas基础的主要内容,如果未能解决你的问题,请参考以下文章
Python基础库及机器学习笔记
python基础学习笔记(十三)
笔记进行目录
20180426学习python笔记(pandas使用)
20180428学习python笔记(pandas使用----绘图)
Python Panda - 学习笔记