【pandas】15 pandas数据结构

本文详细介绍了pandas中的数据结构,包括Series和DataFrame。Series是一种类似于一维数组的对象,支持标签数据。DataFrame是二维表格型数据结构,包含行和列索引。文章讨论了如何创建和读取DataFrame,以及如何通过.loc和.iloc选取数据。还涵盖了多索引数据处理、数据分组Groupby的操作,如聚合计算和循环处理。
摘要由CSDN通过智能技术生成

【pandas】15 pandas数据结构

2023.1.13 总结来自https://mofanpy.com/tutorials/data-manipulation/pandas/
包括pandas数据结构Series/DataFrame;数据选取分类查询等内容

15.1 为什么需要pandas

  • 前面讲了numpy,我们发现,numpy主要用途就是对同一类数据进行处理,可以看成数据矩阵
  • Pandas 主要用于一维二维数据,但用于处理数据的功能也比较多,信息种类也更丰富,特别是你有一些包含字符的表格,Pandas 可以帮你处理分析这些字符型的数据表。
  • Pandas继承了numpy所有优点,但比numpy慢点

pandas中的数据是什么?

  • 数据序列Series
  • 数据表DataFrame

15.2 Series

Series核心就是一串类似于列表的序列。Pandas Series 为数据提供了一套索引。

pandas.Series( data, index, dtype, name, copy)

  • data:一组数据(ndarray 类型)。

  • index:数据索引标签,如果不指定,默认从 0 开始。

  • dtype:数据类型,默认会自己判断。

  • name:设置名称。

  • copy:拷贝数据,默认为 False。

1. 【列表转换】:列表外套个pd.Series() `

import pandas as pd

l = [11,22,33]
s = pd.Series(l)
print(s)
0    11
1    22
2    33
dtype: int64

他这个pandas是按照excel表的习惯来的,一般数据一横行是一个数据,每个列是不同特征
所以这里面其实是三个数据,同一个特征数据,所以是3*1

2. 【index换索引】:'a’是字符串所以要‘’

s = pd.Series(l,index=['a',1,'c'])
print(s)
a    11
1    22
c    33
dtype: int64

3. 【字典创建】:最常规的创建,也是pandas的特色

s=pd.Series({
   "a":11,"b":22,'c':33})
s
a    11
b    22
c    33
dtype: int64

4. 转化成list,Series

print("array:", s.to_numpy())
print("list:", s.values.tolist())
array: [11 22 33]
list: [11, 22, 33]

【5. 访问】:5.1 单独访问 5.2 部分访问

  • 5.1 单独访问
s
0    11
1    22
2    33
dtype: int64
#通过索引访问
s[2]
33
s.at[2]
33
  • 5.2 部分访问:只需要指定需要数据的索引即可
pd.Series(s, index = [0, 2])
0    11
2    33
dtype: int64

6. name

import pandas as pd

sites = {
   1: "Google", 2: "Runoob", 3: "Wiki"}

myvar = pd.Series(sites, index = [1, 2], name="RUNOOB-Series-TEST" )

print(myvar)
1    Google
2    Runoob
Name: RUNOOB-Series-TEST, dtype: object

15.3 数据表DataFrame

  1. pandas.DataFrame( data, index, columns, dtype, copy)
  • data:一组数据(ndarray、series, map, lists, dict 等类型)。

  • index:索引值,或者可以称为行标签。

  • columns:列标签,默认为 RangeIndex (0, 1, 2, …, n) 。

  • dtype:数据类型。

  • copy:拷贝数据,默认为 False。

  1. 横的叫row,竖的叫column

在这里插入图片描述

15.3.1 创建DataFrame数据

1. 【从二维list转成Dataframe】

df = pd.DataFrame([
  [1,2],
  [3,4]
])
df
0 1
0 1 2
1 3 4
  1. 【字典创建】:字典创建的key是列索引,这点和Series有区别,相当于属性;每行习惯看成一条完整的数据
df=pd.DataFrame({
   "col1":[1,3],"col2":[2,4]},index=["a","b"])
df
col1 col2
a 1 2
b 3 4
  • index更改了行索引
  1. 【创建】:也可以把两个series拼起来
s=pd.DataFrame({
   "col1":pd.Series([1,2,6]),"col2":pd.Series([3,4])})
s
col1 col2
0 1 3.0
1 2 4.0
2 6 NaN
  • Series拼接方式索引无法直接更改】应该先拼接再更改
s=pd.DataFrame({
   "col1":pd.Series([1,2]),"col2":pd.Series([3,4])},index=["a","b"])
s
col1 col2
a NaN NaN
b NaN NaN
  1. 更换行索引或列索引:.index.columns
s=pd.DataFrame({
   "'col1'":pd.Series([1,2]),
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

羊老羊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值