pandas 框架数据处理学习笔记1

# encoding=utf-8
#  Series
#       数组与标签
#       可以通过标签选取数据
#       定长的有序字典
#   Dataframe
#       表格型数据结构
#       行索引、列索引
import numpy as np
import pandas as pd

###pandas
# Series
obj = pd.Series([4, 7, -5, 3])
print obj

obj.values
obj.index

obj2 = pd.Series([4, 7, -5, 3], index=['d', 'b', 'a', 'c'])
obj2
obj2.index

obj2 = pd.Series([4, 7, -5, 3], index=['d', 'b', 'a', 'c'])
print obj2
print np.exp(obj2)

print obj2['d']
obj2['d'] = 6
print obj2['d']
print obj2['a']
print 'test1'
print obj2[['c', 'a', 'd']]
print 'test2'
print obj2[obj2 > 0]
print obj2 * 2
print 'test3'
obj2['d'] = 1
print np.exp(obj2)
print 'b' in obj2
print 'c' in obj2
print 'e' in obj2

sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
obj3 = pd.Series(sdata)
print obj3

states = ['California', 'Ohio', 'Oregon', 'Texas']
# print sdata[states]
print 'test4'
obj4 = pd.Series(sdata, index=states)
print obj4
#  查看是否缺失,如果缺失返回true,否则返回false
print pd.isnull(obj4)
print pd.notnull(obj4)
print 'obj4 is null:', obj4.isnull()
print 'obj4 is not null:', obj4.notnull()
print 'obj3=', obj3
print 'obj4=', obj4
print obj3 + obj4
obj4.name = 'population'
obj4.index.name = 'state'
print 'obj4获得population:', obj4
print 'obj:', obj
obj.index = ['Bob', 'Steve', 'Jeff', 'Ryan']
print 'obj:', obj
print 'test5'
# dataframe
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada'],
        'year': [2000, 2001, 2002, 2001, 2002],
        'pop': [1.5, 1.7, 3.6, 2.4, 2.9]}

frame = pd.DataFrame(data)
print 'frame:\n', frame
frame1 = pd.DataFrame(data, columns=['year', 'state', 'pop', 'debt'], index=['one', 'two', 'three', 'four', 'five'])
print 'frame1:\n', frame1
print 'frame1.column', frame1.columns
print 'frame1.index', frame1.index
print 'frame1[\'state\']', frame1['state']
print 'frame1.year\n', frame1.year
print '获取行\n', frame1.ix['three']
frame1['debt'] = 16.5
print 'frame1:=\n', frame1

frame1['debt'] = np.arange(5.)
print frame1

val = pd.Series([-1.2, -1.5, -1.7], index=['two', 'four', 'five'])
frame1['debt'] = val
print frame1
print 'test'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值