pandas基础用法——索引

# -*- coding: utf-8 -*-
# Time    : 2016/11/28 15:14
# Author  : XiaoDeng
# version : python3.5
# Software: PyCharm Community Edition

import pandas as pd
import numpy as np
import matplotlib.pyplot  as  plt

obj=pd.Series(np.arange(4.),index=['a','b','c','d'])
# print(obj)
"""
a    0.0
b    1.0
c    2.0
d    3.0
dtype: float64
"""


#索引用法
print(obj['a'])
print(obj[1])


#索引之切片用法
print('----'*5)
print(obj[2:4])
print(obj[['a','b']])


#取特定索引,可以不连续的索引
print('----'*5)
print(obj[[1,3]])
#取索引小于2个数据
print(obj[obj<2])

 

# -*- coding: utf-8 -*-
# Time    : 2016/11/28 15:14
# Author  : XiaoDeng
# version : python3.5
# Software: PyCharm Community Edition


import pandas as pd
import numpy as np
import matplotlib.pyplot  as  plt


data=pd.DataFrame(np.arange(16).reshape(4,4),
index=['ohio','colorado','utah','newyork'],
columns=['one','two','three','four'])

print(data)


#索引基本用法
print('----'*5)
print(data['two'])
print('----'*5)
print(data[['two','one']])


#索引方式
print('----'*5)
print(data[0:2])


#类似条件语句方式
#查找two列数据大于5的所有数据
print('----'*5)
print(data[data['two']>5])

#对data中所有值小于5的值,重新统一赋值为0
print('----'*5)
data[data<5]=0
print(data)
"""
          one  two  three  four
ohio        0    0      0     0
colorado    0    5      6     7
utah        8    9     10    11
newyork    12   13     14    15
"""

#对行和列同时索引/
# data.ix[行索引名,[列名,列名]]
print('----'*5)
print(data.ix['colorado',['two','four']])
"""
two     5
four    7
Name: colorado, dtype: int32
"""

print('----'*5)
# data.ix[[行索引名,行索引名],[列索引,列索引,列索引]]
s=data.ix[['colorado','ohio'],[3,0,1]]
print(s)
"""
          four  one  two
colorado     7    0    5
ohio         0    0    0
"""

print('----'*5)
print(data.ix[2])   #行索引,索引为2个数据

print(data)
print('----'*5)
# 行索引取utah前的行,列取two列的数据//
print(data.ix[:'utah','two'])

#同时满足2个条件
#1、data.three>5的数据
#2、列索引2之前的数据
#3、如此形成数据的交叉
print('----'*5)
print(data.ix[data.three>5,:2])

 

posted @ 2016-11-28 15:30  Xiao|Deng  阅读(4732)  评论(0编辑  收藏  举报