pandas之两大数据结构

Series

简介

其字符串表现形式为:索引在左边,值在右边

当没有为其指定数据索引时,系统会自动创建一个[0->N-1]的索引

创建

'''
Description: pandas-series
Autor: 365JHWZGo
Date: 2021-11-20 21:24:42
LastEditors: 365JHWZGo
LastEditTime: 2021-11-20 21:32:18
'''
import pandas as pd
import numpy as np
import string
index = np.array([ i for i in string.ascii_uppercase[:10]])
arr = pd.Series(np.linspace(1,10,10),index)
print('index\tvalue')
print(arr)

在这里插入图片描述

DataFrame

简介

DataFrame是一个表格型的数据结构,它包含有一组有序的列,每列可以是不同的值类型(数值,字符串,布尔值等)。DataFrame既有行索引也有列索引, 它可以被看做由Series组成的大字典。

创建

#DataFrame
dates = pd.date_range('2021-11-11','2021-11-20')
col = np.array([ i for i in string.ascii_uppercase[:5]])
print(col.shape)
row = np.random.normal(0,1,(10,5))
print(row.shape)
calendar = pd.DataFrame(row,index=dates,columns=col)
print(calendar)

在这里插入图片描述

截取其中的某一列

#截取其中的某一列
colA = calendar['A']
print('截取A这一列的数据:\n',colA)

运行结果:
截取A这一列的数据:
2021-11-11 -0.417777
2021-11-12 -0.777150
2021-11-13 2.078316
2021-11-14 -0.014898
2021-11-15 -0.024757
2021-11-16 -2.470878
2021-11-17 -0.470804
2021-11-18 0.976633
2021-11-19 -0.926468
2021-11-20 0.811022
Freq: D, Name: A, dtype: float64

查看每一列的属性

#查看每一列的属性
print('每一列的属性:\n',calendar.dtypes)

运行结果:
每一列的属性:
A float64
B float64
C float64
D float64
E float64
dtype: object

查看列序号

#查看序列号
print('列序号:\n',calendar.index)

运行结果:
列序号:
DatetimeIndex([‘2021-11-11’, ‘2021-11-12’, ‘2021-11-13’, ‘2021-11-14’,
‘2021-11-15’, ‘2021-11-16’, ‘2021-11-17’, ‘2021-11-18’,
‘2021-11-19’, ‘2021-11-20’],
dtype=‘datetime64[ns]’, freq=‘D’)

查看数据的名称

#查看数据的名称
print('查看数据的名称:\n',calendar.columns)

运行结果:
查看数据的名称:
Index([‘A’, ‘B’, ‘C’, ‘D’, ‘E’], dtype=‘object’)

查看数据的值

#查看数据的值
print('查看数据的值:\n',calendar.values)

运行结果:
查看数据的值:
[[ 0.70263177 -0.10269037 0.14956381 0.05229755 -0.22693166]
[ 0.76996483 -0.06820687 0.23018498 1.64736412 1.21315162]
[-0.58007419 -1.70054665 0.39012601 -0.400601 -0.95380973]
[-0.34986497 -0.0593376 -0.13585082 0.2477256 0.52293812]
[-0.96190532 0.3854273 -0.18795319 1.16769576 0.61019295]
[-0.7469977 0.72235841 0.84877917 0.07662794 -1.25175773]
[ 1.10971377 -0.88628163 -0.35313278 -1.94813503 1.06510399]
[ 1.31156922 0.62661723 1.38946123 0.98400298 -1.11067423]
[-0.57255518 -0.4666857 0.14901663 -0.09833283 -1.83677056]
[-0.67615395 1.31119262 -1.5026992 0.784727 -0.32706785]]

查看数据的总结

#查看数据的总结
print('查看数据的总结:\n',calendar.describe())

运行结果:
查看数据的总结:
------------ A ---------- B------------- C------------ D------------- E
count 10.000000 10.000000 10.000000 10.000000 10.000000
mean 0.434729 0.515506 -0.174150 -0.387321 0.300599
std 0.857984 0.554675 0.800945 1.026752 1.072510
min -0.960032 -0.352045 -1.157387 -1.768603 -1.193945
25% 0.143484 0.179912 -0.568517 -1.210328 -0.329029
50% 0.498844 0.498011 -0.319065 -0.194878 0.121178
75% 0.901492 0.754429 0.219759 0.169226 1.183275
max 1.641398 1.473177 1.548155 1.568150 2.084425

查看数据的翻转

#查看数据的翻转
print('数据的翻转1:\n',calendar.T)
print('数据的翻转2:\n',calendar.transpose())

运行结果:
数据的翻转1:
2021-11-11 2021-11-12 2021-11-13 … 2021-11-18 2021-11-19 2021-11-20
A 2.337185 -0.132557 -0.666679 … -1.214587 -0.823657 -0.385564
B -2.186472 0.667874 1.102694 … -0.585279 0.326170 -0.008432
C 0.362743 -0.045166 0.091491 … 1.465950 0.664857 0.422505
D -1.572811 0.184403 0.591719 … -0.387301 -0.206930 -0.440639
E -1.189427 1.386185 0.336455 … 0.248305 -1.858573 -0.214051

[5 rows x 10 columns]
数据的翻转2:
2021-11-11 2021-11-12 2021-11-13 … 2021-11-18 2021-11-19 2021-11-20
A 2.337185 -0.132557 -0.666679 … -1.214587 -0.823657 -0.385564
B -2.186472 0.667874 1.102694 … -0.585279 0.326170 -0.008432
C 0.362743 -0.045166 0.091491 … 1.465950 0.664857 0.422505
D -1.572811 0.184403 0.591719 … -0.387301 -0.206930 -0.440639
E -1.189427 1.386185 0.336455 … 0.248305 -1.858573 -0.214051

[5 rows x 10 columns]

查看对索引排序输出

#DataFrame
dates = pd.date_range('2021-11-11','2021-11-20')

a = np.random.choice([i for i in string.ascii_uppercase[:5]],5,replace=False)
row = np.random.normal(0,1,(10,5))
calendar = pd.DataFrame(row,index=dates,columns=a)
print('calendar:\n',calendar)

#查看对索引排序输出
print('按索引排序输出:\n',calendar.sort_index(axis=1,ascending=True))

运行结果:
在这里插入图片描述

查看对值排序输出

#查看对值排序输出
print('按对应列的值排序输出:\n',calendar.sort_values(by='D'))

运行结果:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

365JHWZGo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值