pandas---数据结构(Series、DataFrame 和 MultiIndex)创建方式、属性

1. 数据结构

Pandas中一共有三种数据结构,分别为:Series、DataFrame 和MultiIndex。

其中Series是一维数据结构,DataFrame是二维表格型数据结构,MultiIndex是三维数据结构。

1.1 Series

Series是一个类似于一维数组的数据结构,它能够保存任何类型的数据,比如整数、字符串、浮点

数等,主要由一组数据和与之相关的索引两部分构成。

 Series的创建:

# 导入pandas
import pandas as pd
pd.Series(data=None, index=None, dtype=None)

参数: data:传入的数据,可以是ndarray、list等

index:索引,必须是唯一的,且与数据的长度相等。如果没有传入索引参数,则默认会自动创建

一个从0-N的整数索引。

dtype:数据的类型

通过已有数据创建:

指定内容,默认索引:

pd.Series(np.arange(10))
# 运行结果
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
dtype: int64

指定索引:

pd.Series([6.7,5.6,3,10,2], index=[1,2,3,4,5])
# 运行结果
1 6.7
2 5.6
3 3.0
4 10.0
5 2.0
dtype: float64

通过字典数据创建:

color_count = pd.Series({'red':100, 'blue':200, 'green': 500, 'yellow':1000})
color_count
# 运行结果
blue 200
green 500
red 100
yellow 1000
dtype: int64

Series的属性

为了更方便地操作Series对象中的索引和数据,Series中提供了两个属性index和values。

color_count.index
# 结果
Index(['blue', 'green', 'red', 'yellow'], dtype='object')
color_count.values
# 结果
array([ 200, 500, 100, 1000])
# 也可以使用索引来获取数据:
color_count[2]
# 结果
100

1.2 DataFrame

DataFrame是一个类似于二维数组或表格(如excel)的对象,既有行索引,又有列索引。

行索引,表明不同行,横向索引,叫index,0轴,axis=0;

列索引,表名不同列,纵向索引,叫columns,1轴,axis=1。

DataFrame的创建:

# 导入pandas
import pandas as pd
pd.DataFrame(data=None, index=None, columns=None)

参数:index:行标签。如果没有传入索引参数,则默认会自动创建一个从0-N的整数索引。

columns:列标签。如果没有传入索引参数,则默认会自动创建一个从0-N的整数索引。

通过已有数据创建:

pd.DataFrame(np.random.randn(2,3))

# 构造行索引序列
subjects = ["语文", "数学", "英语", "政治", "体育"]
# 构造列索引序列
stu = ['同学' + str(i) for i in range(score_df.shape[0])]
# 添加行索引
data = pd.DataFrame(score, columns=subjects, index=stu)

DataFrame的属性:

shape、index、values、columns、T

data.shape
# 结果
(10, 5)
data.index
# 结果
Index(['同学0', '同学1', '同学2', '同学3', '同学4', '同学5', '同学6', '同学7', '同学8', '同学9'], dtype='object')
data.columns
# 结果
Index(['语文', '数学', '英语', '政治', '体育'], dtype='object')
data.values
array([[92, 55, 78, 50, 50],
[71, 76, 50, 48, 96],
[45, 84, 78, 51, 68],
[81, 91, 56, 54, 76],
[86, 66, 77, 67, 95],
[46, 86, 56, 61, 99],
[46, 95, 44, 46, 56],
[80, 50, 45, 65, 57],
[41, 93, 90, 41, 97],
[65, 83, 57, 57, 40]])
data.T

head(5):显示前5行内容

如果不补充参数,默认5行。填入参数N则显示前N行。

tail(5):显示后5行内容

如果不补充参数,默认5行。填入参数N则显示后N行。

DatatFrame索引的设置:

修改行列索引值:

stu = ["学生_" + str(i) for i in range(score_df.shape[0])]
# 必须整体全部修改
data.index = stu

重设索引:

reset_index(drop=False) 设置新的下标索引

drop:默认为False,不删除原来索引,如果为True,删除原来的索引值。

 以某列值设置为新的索引:

set_index(keys, drop=True) keys : 列索引名成或者列索引名称的列表

drop : boolean, default True。当做新的索引,删除原来的列。

df = pd.DataFrame({'month': [1, 4, 7, 10],
'year': [2012, 2014, 2013, 2014],
'sale':[55, 40, 84, 31]})
month sale year
0 1 55 2012
1 4 40 2014
2 7 84 2013
3 10 31 2014
# 以月份设置新的索引
df.set_index('month')
sale year
month
1 55 2012
4 40 2014
7 84 2013
10 31 2014
# 设置多个索引,以年和月份
df = df.set_index(['year', 'month'])
df
sale
year month
2012 1 55
2014 4 40
2013 7 84
2014 10 31

注意:通过刚才的设置,这样DataFrame就变成了一个具有MultiIndex的DataFrame。

1.3 MultiIndex

MultiIndex是三维的数据结构, 多级索引(也称层次化索引)是pandas的重要功能,可以在

Series、DataFrame对象上拥有2个以及2个以上的索引。

multiIndex的创建:

arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]
pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))
# 结果
MultiIndex(levels=[[1, 2], ['blue', 'red']],
codes=[[0, 0, 1, 1], [1, 0, 1, 0]],
names=['number', 'color'])

index属性;names:levels的名称;levels:每个level的元组值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三月七꧁ ꧂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值