Pandas系列学习教程(五)

Pandas系列教程(五)


  对于想要入门数据科学的朋友们来说,Python是一个很好的选择,除了因为简单的语法外,Python 生态中提供了很多在数值计算方面非常优秀的库,其中Pandas不可不提,Pandas是很强大是数据集处理工具,往往和numpy, matplotlib 等库搭配使用,我也是刚刚开始学习Pandas, 顺便翻译了一下官方的Pandas教程, 这里使用的是jupyter notebook, 因为博客不支持html直接编辑,所以只能转化为markdown 格式,如果想直接查看html版本可点击每一节下的链接。本文仅供学习和交流使用,欢迎大家交流和指正!


摘要

  • 字典式定义数据集
  • pandas中的index索引以及变换
  • df.T transpose转置操作

HTML版本点击此处

# 导入包
import pandas as pd
import sys

print('Python version:',sys.version)
print('pandas version:',pd.__version__)
Python version: 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0]
pandas version: 0.23.0
# 数据集,注意这里创建的方式类似于字典
d = {'one':[1,1],'two':[2,2]}
i = ['a','b']

# 创建数据帧
df = pd.DataFrame(data=d,index=i)
df
onetwo
a12
b12
df.index
Index(['a', 'b'], dtype='object')
# 获得列的值
stack = df.stack()
stack
a  one    1
   two    2
b  one    1
   two    2
dtype: int64
# 获得stack的索引,多重索引
stack.index
MultiIndex(levels=[['a', 'b'], ['one', 'two']],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
# 逐个获取行的值
unstack = df.unstack()
unstack
one  a    1
     b    1
two  a    2
     b    2
dtype: int64
unstack.index
MultiIndex(levels=[['one', 'two'], ['a', 'b']],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]])
# 将数据帧进行转置
transpose = df.T
transpose
ab
one11
two22
transpose.index
Index(['one', 'two'], dtype='object')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值