Pandas学习:对于Series和DataFrame一些基本操作

本文主要讲述一些对于Series和DataFrame一些基本的操作,具体用法说明见程序中的注释:

代码:

#coding=utf-8
'''
Created on 2017-2-23

@author: admin
'''
from pandas import Series
from pandas import DataFrame
import numpy as np

obj=Series([1,3,2,4],index=['a','e','b','d'])
print obj

print 'reindex方法重新根据索引进行排序'
print obj.reindex(['a','b','e','d'])
print '丢弃obj中的某项:'
print obj.drop('e')
print '索引:'
print obj['a'],obj[0]
print obj[0:2]
#查找series中小于3的值都有哪些
print obj[obj<3]

print 'reindex在dataframe中进行索引排序:'
frame=DataFrame(np.arange(9).reshape(3,3),index=[0,1,2],columns=['b','a','c'])
print frame
print '用reindex将dataframe中行列索引进行重新排列:'
print frame.reindex([1,0,2],columns=['a','b','c'])
print '丢弃dataframe中某行数据:'
print frame.drop([0],axis=0)
print '丢弃dataframe中某列数据:'
print frame.drop(['a'],axis=1)
print '索引:'
print frame.ix[[0,1],['a','b']]

实验结果:

a    1
e    3
b    2
d    4
dtype: int64
reindex方法重新根据索引进行排序
a    1
b    2
e    3
d    4
dtype: int64
丢弃obj中的某项:
a    1
b    2
d    4
dtype: int64
索引:
1 1
a    1
e    3
dtype: int64
a    1
b    2
dtype: int64
reindex在dataframe中进行索引排序:
   b  a  c
0  0  1  2
1  3  4  5
2  6  7  8
用reindex将dataframe中行列索引进行重新排列:
   a  b  c
1  4  3  5
0  1  0  2
2  7  6  8
丢弃dataframe中某行数据:
   b  a  c
1  3  4  5
2  6  7  8
丢弃dataframe中某列数据:
   b  c
0  0  2
1  3  5
2  6  8
索引:
   a  b
0  1  0
1  4  3


本文完全原创,如有雷同,纯属你抄我。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值