pandas 基础

serise

import pandas as pd
from pandas import Series, DataFrame
obj = Series([, -, , ])
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   -7
3    3
dtype: int64
obj[:]
1   -7
2    5
dtype: int64
pd.isnull(obj)
0    False
1    False
2    False
3    False
dtype: bool
  • reindex可以用来插值
obj.reindex(range(), method = 'ffill')
0    4
1   -7
2    5
3    3
4    3
dtype: int64
  • 标签切片是闭区间的

dataframe

data = {'state': ['asd','qwe','sdf','ert'],
       'year': [, , , ],
       'pop': [1.5,1.7,3.6,2.4]}
data = DataFrame(data)
data
popstateyear
01.5asd2000
11.7qwe2001
23.6sdf2002
32.4ert2003
data.year
# 比r里提取列要方便点
0    2000
1    2001
2    2002
3    2003
Name: year, dtype: int64
data['debt'] = range()
data
popstateyeardebt
01.5asd20000
11.7qwe20011
23.6sdf20022
32.4ert20033
  • 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[:]
popstateyeardebt
01.5asd20000
11.7qwe20011
23.6sdf20022
data.ix[:,:]
popstateyear
01.5asd2000
11.7qwe2001
23.6sdf2002
32.4ert2003
  • 删除某列用drop,axis = 0表示行,1表示列
  • 删除后原数据不变
data.drop(,axis=)
popstateyeardebt
11.7qwe20011
23.6sdf20022
32.4ert20033
data.drop('year', axis=)
popstatedebt
01.5asd0
11.7qwe1
23.6sdf2
32.4ert3
data
popstateyeardebt
01.5asd20000
11.7qwe20011
23.6sdf20022
32.4ert20033
import numpy as np
df = DataFrame(np.arange().reshape(, ))
df
012
0012
1345
2678
  • applymap()可以对dataframe每一个元素运用函数
  • apply()可以对每一维数组运用函数
df.applymap(lambda x: '%.2f' % x)
012
00.001.002.00
13.004.005.00
26.007.008.00
data.sort_values(by='pop')
# 对某一列排序
popstateyeardebt
01.5asd20000
11.7qwe20011
32.4ert20033
23.6sdf20022
data.describe()
popyeardebt
count4.0000004.0000004.000000
mean2.3000002001.5000001.500000
std0.9486831.2909941.290994
min1.5000002000.0000000.000000
25%1.6500002000.7500000.750000
50%2.0500002001.5000001.500000
75%2.7000002002.2500002.250000
max3.6000002003.0000003.000000
df.isin([])
012
0FalseTrueFalse
1FalseFalseFalse
2FalseFalseFalse
  • None、NaN会被当作NA处理
  • df.shape不加括号相当于dim()
df.shape
(3, 3)
  • dropna删除缺失值
df.ix[:, :] = None
df
012
0NaNNaN2
1NaNNaN5
26.07.08
  • 填充缺失值可以调用字典,不同行添加不同值
df.fillna({0:11, 1:22})
012
011.022.02
111.022.05
26.07.08
df
012
0NaNNaN2
1NaNNaN5
26.07.08
df.fillna({0:11, 1:22}, inplace=True)
012
011.022.02
111.022.05
26.07.08
df
012
011.022.02
111.022.05
26.07.08
  • inplace修改对象不产生副本