pandas的DataFrame

这篇博客介绍了pandas库中的DataFrame数据结构,它建立在numpy基础上,由多个Series构成,共享同一个index。DataFrame的列索引包含了所有Series的列名,并且行索引和列索引都具有name属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

numpy

# 查看数组类型
np.dtype
# 二维数组建立
a_np=np.array([[1,3,5],[5,3,5],[44,35,55]])
# axis=0按列统计,axis=1按行统计

以numpy为基础的pandas(建立在数组之上).series

#创建series
import pandas as pd
a_series=pd.Series(['a','v','d'])
a_series.index.name='行索引'
# 设置index
b_series=pd.Series(['1','e','v'],index=['3','3','e'])
# 数据内容
a_series.values
# 行索引
a_series.index
# 行索引的名称
a_series.index.name
# series元素查找
# iloc绝对位置查找
a_series.iloc[0]
# loc索引查找
b_series.loc['第二个元素']
# series的元素个数
a_series.count()
# series的统计信息
a_series.describe()
# series的元素类型
a_series.dtype
# 两向量按照index相同进行相加,无法相加的显示NaN
c_series=a_series+b_series

DataFrame(由若干Series组成,共享一个index,列索引包含所有Series的列名称,列索引行索引均有name属性)

# df.values  数据内容
# df.index 行索引  df.index.name 行索引名字
# df.columns 列索引 df.columns.name  列索引名字
# 字典的key对应columns,
# 根据字典构造DataFrame
from collection import OrderedDict  # 引入有序字典
score_ord_dict=OrderedDict(score_dict) # 将字典转换为有序格式
score_df=DataFrame(score_ord_dict) # 将有序字典转换为DataFrame
# 统计功能,按列计算(每列数据的个数)
df.count()
# 描述性统计信息
df.describe()
# 查看数据类型
df.dtypes()
# 查看数据框大小
df.shape()
# iloc[行标,列标]
df.iloc[:,2]
# loc[行索引,列索引](包含尾列)
df.loc[['a','v'],['d','a']]
# 筛选行数据
df.loc[bool型数据,:]

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值