栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Pandas两种数据类型(jupyter)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Pandas两种数据类型(jupyter)

import pandas as pd
import numpy as np
# pandas基本数据结构
# Series 一维数组,与numpy中的一维array相似,二者与pyhton列表也相近。Series能保存不同数据类型,字符串,boolean值,数字等都能保存在series中
# Dateframe 二维的表格型数据结构。很多功能与R中date.frame类似。可以将DateFrame理解为Series的容器。以下的内容主要以DateFrame为主

# Series
# 一维Series可以用一维列表初始化; 索引可以自己设置
s = pd.Series([1,2,3])
'''0    1
   1    2
   2    3
   dtype: int64'''
s = pd.Series([1,2,4],index =['a','b','c'])
'''a    1
   b    2
   c    4
   dtype: int64'''
# 当使用其他字符作为索引时,切片操作为双闭合,两边端点元素都要取
# 查看数据索引:行标签
s.index  # Index(['a', 'b', 'c'], dtype='object')
s.values  # array([1, 2, 4], dtype=int64)
# 索引赋值
s.index.name='索引'
'''索引
   a    1
   b    2
   c    4
   dtype: int64'''
s.index=list('012')
'''0    1
   1    2
   2    3
   dtype: int64'''

# DateFrame类型 这是个二维结构,这里首先构建一组时间序列,作为我们第一维的下标:
date = pd.date_range('20220423',periods=5)
'''DatetimeIndex(['2022-04-23', '2022-04-24', '2022-04-25', '2022-04-26',
                  '2022-04-27'],
                  dtype='datetime64[ns]', freq='D')'''

a = np.random.randint(1,10,30)
a.shape = 5,6
DF = pd.DataFrame(a,index=date,columns=list('ABCDEF')) # 第一个参数为二维数组,index,columns为索引,默认情况下他们的值由0开始的数字替代
'''            A  B  C  D  E  F
   2022-04-23  9  4  8  4  3  1
   2022-04-24  3  7  2  7  2  8
   2022-04-25  1  3  4  5  1  3
   2022-04-26  3  5  8  1  3  2
   2022-04-27  5  3  2  4  6  9'''

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/829483.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号