Pandas数据操作

Pandas数据操作

import pandas as pd

Series索引

ser_obj = pd.Series(range(5), index = ['a', 'b', 'c', 'd', 'e'])
print(ser_obj.head())
a    0
b    1
c    2
d    3
e    4
dtype: int32

行索引

print(ser_obj['a'])
print(ser_obj[0])
0
0

切片索引

print(ser_obj[1:3])
print(ser_obj['b':'d'])
b    1
c    2
dtype: int32
b    1
c    2
d    3
dtype: int32

不连续索引

print(ser_obj[[0, 2, 4]])
print(ser_obj[['a', 'e']])
a    0
c    2
e    4
dtype: int32
a    0
e    4
dtype: int32

布尔索引

ser_bool = ser_obj > 2
print(ser_bool)
print(ser_obj[ser_bool])

print(ser_obj[ser_obj > 2])
a    False
b    False
c    False
d     True
e     True
dtype: bool
d    3
e    4
dtype: int32
d    3
e    4
dtype: int32

DataFrame索引

import numpy as np

df_obj = pd.DataFrame(np.random.randn(5,4), columns = ['a', 'b', 'c', 'd'])
print(df_obj.head())
          a         b         c         d
0 -0.255086 -1.605135 -0.491771  0.147356
1 -0.870266 -0.495241 -0.077998  1.017201
2  1.146990 -1.016143 -0.829765  3.012885
3 -0.331168  0.183293  0.898056 -0.595689
4  0.048942  1.577595 -0.980013  1.382445

列索引

print('列索引')
print(df_obj['a']) # 返回Series类型
print(type(df_obj.loc[[0]])) # 返回DataFrame类型
列索引
0   -0.255086
1   -0.870266
2    1.146990
3   -0.331168
4    0.048942
Name: a, dtype: float64
<class 'pandas.core.frame.DataFrame'>

不连续索引

print('不连续索引')
print(df_obj[['a','c']])
print(df_obj.iloc[[1, 3]])
不连续索引
          a         c
0 -0.255086 -0.491771
1 -0.870266 -0.077998
2  1.146990 -0.829765
3 -0.331168  0.898056
4  0.048942 -0.980013
          a         b         c         d
1 -0.870266 -0.495241 -0.077998  1.017201
3 -0.331168  0.183293  0.898056 -0.595689

三种索引方式

标签索引 loc

# Series
print(ser_obj['b':'d'])
print(ser_obj.loc['b':'d'])

# DataFrame
print(df_obj['a'])
print(df_obj.loc[0:2, 'a'])
b    1
c    2
d    3
dtype: int32
b    1
c    2
d    3
dtype: int32
0   -0.255086
1   -0.870266
2    1.146990
3   -0.331168
4    0.048942
Name: a, dtype: float64
0   -0.255086
1   -0.870266
2    1.146990
Name: a, dtype: float64

整型位置索引 iloc

print(ser_obj[1:3])
print(ser_obj.iloc[1:3])

# DataFrame
print(df_obj.iloc[0:2, 0]) # 注意和df_obj.loc[0:2, 'a']的区别
b    1
c    2
dtype: int32
b    1
c    2
dtype: int32
0   -0.255086
1   -0.870266
Name: a, dtype: float64

混合索引 ix

print(ser_obj.ix[1:3])
print(ser_obj.ix['b':'c'])

# DataFrame
print(df_obj.ix[0:2, 0]) # 先按标签索引尝试操作,然后再按位置索引尝试操作
b    1
c    2
dtype: int32
b    1
c    2
dtype: int32
0   -0.255086
1   -0.870266
2    1.146990
Name: a, dtype: float64


C:\Users\weixiao\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  """Entry point for launching an IPython kernel.
C:\Users\weixiao\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: DeprecationWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecate_ix
  """

运算与对齐

s1 = pd.Series(range(10, 20), index = range(10))
s2 = pd.Series(range(20, 25), index = range(5))

print('s1: ' )
print(s1)

print('') 

print('s2: ')
print(s2)
s1: 
0    10
1    11
2    12
3    13
4    14
5    15
6    16
7    17
8    18
9    19
dtype: int32

s2: 
0    20
1    21
2    22
3    23
4    24
dtype: int32

Series 对齐运算

s1 + s2
0    30.0
1    32.0
2    34.0
3    36.0
4    38.0
5     NaN
6     NaN
7     NaN
8     NaN
9     NaN
dtype: float64
import numpy as np

df1 = pd.DataFrame(np.ones((2,2)), columns = ['a', 'b'])
df2 = pd.DataFrame(np.ones((3,3)), columns = ['a', 'b', 'c'])

print('df1: ')
print(df1)

print('') 
print('df2: ')
print(df2)
df1: 
     a    b
0  1.0  1.0
1  1.0  1.0

df2: 
     a    b    c
0  1.0  1.0  1.0
1  1.0  1.0  1.0
2  1.0  1.0  1.0

DataFrame对齐操作

df1 + df2
a   b   c

0 2.0 2.0 NaN
1 2.0 2.0 NaN
2 NaN NaN NaN







# 填充未对齐的数据进行运算


```py
print(s1)
print(s2)

s1.add(s2, fill_value = -1)
0    10
1    11
2    12
3    13
4    14
5    15
6    16
7    17
8    18
9    19
dtype: int32
0    20
1    21
2    22
3    23
4    24
dtype: int32





0    30.0
1    32.0
2    34.0
3    36.0
4    38.0
5    14.0
6    15.0
7    16.0
8    17.0
9    18.0
dtype: float64
df1.sub(df2, fill_value = 2.)
a   b   c

0 0.0 0.0 1.0
1 0.0 0.0 1.0
2 1.0 1.0 1.0









# 填充NaN


```py
s3 = s1 + s2
print(s3)
0    30.0
1    32.0
2    34.0
3    36.0
4    38.0
5     NaN
6     NaN
7     NaN
8     NaN
9     NaN
dtype: float64
s3_filled = s3.fillna(-1)
print(s3_filled)
0    30.0
1    32.0
2    34.0
3    36.0
4    38.0
5    -1.0
6    -1.0
7    -1.0
8    -1.0
9    -1.0
dtype: float64
df3 = df1 + df2
print(df3)
     a    b   c
0  2.0  2.0 NaN
1  2.0  2.0 NaN
2  NaN  NaN NaN
df3.fillna(100, inplace = True)
print(df3)
       a      b      c
0    2.0    2.0  100.0
1    2.0    2.0  100.0
2  100.0  100.0  100.0

函数应用

Numpy ufunc 函数

df = pd.DataFrame(np.random.randn(5,4) - 1)
print(df)

print(np.abs(df))
          0         1         2         3
0 -0.155651 -0.610114 -0.237815 -1.395167
1  0.016375 -0.782956 -1.605014 -0.596293
2 -0.749993  0.286530 -0.964842 -2.545031
3  0.754360  1.161125 -1.315330 -2.810557
4 -1.950679 -0.432384 -0.811125 -0.284081
          0         1         2         3
0  0.155651  0.610114  0.237815  1.395167
1  0.016375  0.782956  1.605014  0.596293
2  0.749993  0.286530  0.964842  2.545031
3  0.754360  1.161125  1.315330  2.810557
4  1.950679  0.432384  0.811125  0.284081

使用apply应用行或列数据

#f = lambda x : x.max()
print(df.apply(lambda x : x.max()))
0    0.754360
1    1.161125
2   -0.237815
3   -0.284081
dtype: float64

指定轴方向

print(df.apply(lambda x : x.max(), axis=1))
0   -0.155651
1    0.016375
2    0.286530
3    1.161125
4   -0.284081
dtype: float64

使用applymap应用到每个数据

f2 = lambda x : '%.2f' % x
print(df.applymap(f2))
       0      1      2      3
0  -0.16  -0.61  -0.24  -1.40
1   0.02  -0.78  -1.61  -0.60
2  -0.75   0.29  -0.96  -2.55
3   0.75   1.16  -1.32  -2.81
4  -1.95  -0.43  -0.81  -0.28

排序

s4 = pd.Series(range(10, 15), index = np.random.randint(5, size=5))
print(s4)
3    10
3    11
2    12
3    13
2    14
dtype: int32

索引排序

s4.sort_index()
2    12
2    14
3    10
3    11
3    13
dtype: int32
df4 = pd.DataFrame(np.random.randn(3, 4), 
                   index=np.random.randint(3, size=3),
                   columns=np.random.randint(4, size=4))
print(df4)
          2         2         1         0
0  0.578335 -1.058499 -2.050409  1.033859
2 -0.336811  0.062738  0.993909 -1.329422
1 -0.767846 -0.669763  1.197213 -0.060293
#df4.sort_index(ascending=False)
df4.sort_index(axis=1)

按值排序

df4.sort_values(by=1)

处理缺失数据

df_data = pd.DataFrame([np.random.randn(3), [1., np.nan, np.nan],
                       [4., np.nan, np.nan], [1., np.nan, 2.]])
df_data.head()

isnull

df_data.isnull()

dropna

df_data.dropna()
#df_data.dropna(axis=1)

fillna

df_data.fillna(-100.)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容