自学内容网 自学内容网

yjs09——pandas介绍及相关数据结构

1.什么是pandas

同样,pandas、matplotlib、numpy是python三大库,pandas就像是把matplotlib和numpy结合在一起,让数据以“表格”的形式表现出来,是一个强大的数据处理和分析库,它建立在NumPy库之上,提供了高效地操作大型数据集所需的数据结构和操作。

2.pandas库的引入

import pandas as pd

3.pandas 的数据结构

pandas主要有 Series、DataFrame、MutiIndex/panel三大结构


Series:一位数据结构

DataFrame:二维表格型数据结构

MultiIndex/panel:三维数据结构

Ⅰ.Series结构

        1.Series结构的建立

                a.正常方式建立:table=pd.Series(data,index=)

                b.不写index,默认为0,1,2,3...   table=pd.Series(data)

                c.字典方式建立:table=pd.Series({"key1":value,   "key2":value   ...})

        2.Series属性查看

                table.index   //查看索引名,没有括号

                table.value    //查看索引值,没有括号

        3.Series结构的样子:

                        



Ⅱ.DataFrame结构

1.建立

table=pd.DataFrame(data,index=,columns=)

table=pd.DataFrame({"key1":[v11,v21,v31...]  ,"key2":[v12,v22,v32] ....   })

2.属性

table.shape

table.index

table.columns

table.values

table.head(k)     //输出前k行

table.tail(k)         //输出后k行

3.行列索引的修改

a.更改原来的索引名称

table.index=[]

table.columns=[]

//要么全部修改,要么不修改,不能只修改某一行、列的索引名


b.重设索引(只能重设为0,1,2,3....)
        删除原来的索引,重设0,1,2...索引

        table1=table.reset_index(drop=True)

        不删除原来的索引,将原来的索引归成数据(不传参数时默认)

        table1=table.reset_index(drop=False)

//需要有新表接收,不是在原数据上操作

//只有reset_index,没有reset_columns


c.从原数据中取出一列作为索引
        删除原数据中作为新索引的数据

        table1=table.set_index(["name",''age''],drop=True)

        不删除原数据中作为新索引的数据

        table1=table.set_index("name",drop=False)

//需要有新表接收,不是在原数据上操作

//只有set_index,没有set_columns

//可以有多个索引



Ⅲ.MultiIndex结构

如果table是多索引的表,我们查看他的索引时:

table.index

结果:MultiIndex([(索引1.1, 索引2.1),
            (索引1.2, 索引2.2),
            (索引1.3, 索引2.3)],
           names=['索引1', '索引2'])

table.index.levels 

结果:【[索引1,索引1,索引1...],[索引2,索引2,索引2...]】

这里的索引1,2就不是一一对应的,如果索引1中有重复的,那就只显示一个,一般是按照从小到大排

代码

# pandas笔记
import numpy as np
import pandas as pd
from pandas import DataFrame

# 一、pandas之series数据结构
# 1.series结构的建立
data1 = np.random.randint(60, 100, 4)
index_1 = pd.Series(data1, index=["学生1", "学生2", "学生3", "学生4"])  # Series一定要大写!
# 字典法建立:
index_2 = pd.Series({"学生A": 90, "学生B": 80})
print(index_1)
print(index_2)

"""结果:
学生1    67
学生2    73
学生3    74
学生4    97
dtype: int32
-----------------
学生A    90
学生B    80
dtype: int64
"""

# 2.Series的属性
print(index_1.index)  # Index(['学生1', '学生2', '学生3', '学生4'], dtype='object')
print(index_1.values)  # [81 88 62 75]
print(index_1.head(2))
"""
    学生1    81
    学生2    88
    dtype: int32"""
print(index_1.tail(2))
"""
    学生3    62
    学生4    75
    dtype: int32"""

# 二.DataFrame数据结构
# 2.1建立
table_1 = DataFrame(np.random.randint(10, 25, (3, 4)), index=["上海", "广州", "深圳"],
                    columns=["11月1日", "11月2日", "11月3日", "11月4日"])
table_2 = DataFrame({"上海": [21, 22, 22, 27], "广州": [23, 22, 25, 27], "深圳": [21, 24, 27, 24]},
                    index=["11月1日", "11月2日", "11月3日", "11月4日"])  # 字典的key组成列索引
print(table_1)
print(table_2)
print(table_2.index)
print(table_2.columns)

# 2.2属性
print(table_1.shape)
print(table_1.index)
print(table_1.columns)
print(table_2.T)
print(table_1.head(1))
print(table_1.tail(2))

# 2.3重设索引
table = pd.DataFrame(np.random.randint(60, 100, (3, 4)), index=["张三", "李四", "王五"],
                     columns=["语", "数", "英", "政"])
print(table)

# 2.3.1 table.index
table.index = ["同学" + str(i) for i in range(3)]
print(table)

# 2.3.2 table.reset_index()
table = table.reset_index(drop=True)
print(table)

# 2.3.3 table.set_index("",)
table = table.set_index("语")
print(table)

table0 = table.set_index(["政","英"], drop=False)
print(table0)
print(table0.index)
print(table0.index.levels)

我遇到的问题:

1.数据结构大小写问题

Series、DateFrame、MultiIndex,这三个都是大写首字母

2.DataFrame的引进

from pd import DataFrame

3.索引的重设中,能不能重设列索引

只有table.columns=【】是重设列索引,reset、set_...都是对index改变

4.DataFrame索引问题

reset_index和 set_index都不是在原数据上改变,所以需要有接收的table变量

reset_index只能重设成0,1,2,3...

reset_index和set_index的drop含义不同

index、set_index、reset_index后的结果

5.当索引中有重复时

只显示一个,不重复显示,如果是表,重复的部分空着,如果是看属性结果,那么只输出一个

6.用字典法建立DataFrame结构时

key值是列索引,行索引是0,1,2,3...


原文地址:https://blog.csdn.net/weixin_59924168/article/details/142576175

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!