Pandas与numpy的主要区别在于:Pandas主要用来处理表格和异构数据,numpy更适合处理同类型的数组数据。

        import pandas as pd这是使用pandas必须要执行的语句,导入pandas库并简写为pd,后续使用pandas中的模块必须以pd开始。

        Pandas中的两个主要数据结构是:Series和DataFrame。Series是类似于一维数组的对象,其创建方式为pd.Series(data,index=index)

data:

  • (1)python字典
  • (2)numpy 数组
  • (3)标量值(如整数,浮点数等)

Series的索引(index)是轴标签的列表,用于标识数据的位置。以下是具体说明:

索引是Series中数据点的标签集合,用于访问或操作数据。例如,通过索引可以定位到特定元素(如series['a'])或进行切片操作(如series[:3]

 index:

(1)当数据是ndarray时,index的长度和data必须一致。

import pandas as pd
import numpy as np
df=pd.Series(np.random.rand(6),index=['a','b','c','d','e','f'])
print(df)
a    0.319724
b    0.106982
c    0.870340
d    0.991443
e    0.112509
f    0.792201
dtype: float64

      当两者不一致时,将会出现ValueError,如下所示,数据的长度是7,索引6,提示错误,does not match。

df=pd.Series(np.random.rand(7),index=['a','b','c','d','e','f'])
ValueError: Length of values (7) does not match length of index (6)

     如果不添加索引,则默认为数字索引。

df=pd.Series(np.random.rand(6))
0    0.203116
1    0.627751
2    0.217918
3    0.393877
4    0.234143
5    0.574522
dtype: float64

(2)字典数据(pd.Series()将字典数据转换为Series)

import pandas as pd
import numpy as np
d={"college":"晓庄学院","city":"南京","country":"中国"}
print(pd.Series(d))
college    晓庄学院
city         南京
country      中国
dtype: object

可以通过index参数改变显示顺序:

import pandas as pd
import numpy as np
d={"college":"晓庄学院","city":"南京","country":"中国"}
print(pd.Series(d,index=["country","city","college"]))
country      中国
city         南京
college    晓庄学院
dtype: object

(3)标量值(当赋值为一个标量值的时候,index所对应的数值,全部为一个值)

import pandas as pd
print(pd.Series(88,index=["a","b","c"]))
a    88
b    88
c    88
dtype: int64

        使用Pandas库的首要工作是导入Pandas库,import pandas as pd,有些情况下,可能使用from pandas import Series,DataFrame,后者在后续的使用中,不需要加pd.。示例如下:

import pandas as pd
pd1=pd.Series([1,2,3,4,5,6])
print(pd1)
from pandas import Series,DataFrame
pd1=Series([1,2,3,4,5,6])
print(pd1)

就个人使用习惯来讲,推荐第一种方式。

本次文档实例中,只讨论Series的具体使用情况,DataFrame将在后续文中讨论。

import pandas as pd                    #导入pandas库
data=pd.Series([1,2,3,4,5,6])          #Series是类似一维数组的对象
print(data)                            #左边显示索引,右边显示数据,未指定索引,从0开始
# 0    1
# 1    2
# 2    3
# 3    4
# 4    5
# 5    6
# dtype: int64
print(data.index)
# RangeIndex(start=0, stop=6, step=1)
data_a=pd.Series([1,2,3,4,5,6],index=['a','b','c','d','e','f'])
#创建带有索引的Series,用具体的索引指明Series的值
print(data_a)
# a    1
# b    2
# c    3
# d    4
# e    5
# f    6
# dtype: int64
print(data_a.index)
# Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')
print(data_a['a'])
# 1
print(data_a[['a','c','e']])    #多个索引值,需要用'[]'括起来
# a    1
# c    3
# e    5
# dtype: int64
print(data_a[data_a>3])
# d    4
# e    5
# f    6
# dtype: int64
print(data_a*2)
# a     2
# b     4
# c     6
# d     8
# e    10
# f    12
# dtype: int64
import numpy as np
print(np.exp(data_a))
# a      2.718282
# b      7.389056
# c     20.085537
# d     54.598150
# e    148.413159
# f    403.428793
# dtype: float64
d1={"jiangsu":"nanjing","zhejiang":"hangzhou","hebei":"shijiazhuang"}
pd1=pd.Series(d1)               #将字典转换为Series
print(pd1)
# jiangsu          nanjing
# zhejiang        hangzhou
# hebei       shijiazhuang
# dtype: object
d2=pd1.to_dict()               #将Series转换为dict
print(d2)
# {'jiangsu': 'nanjing', 'zhejiang': 'hangzhou', 'hebei': 'shijiazhuang'}
p=['hebei','zhejiang','jiangsu']
pd2=pd.Series(d1,index=p)   #指定索引顺序
print(pd2)
# hebei       shijiazhuang
# zhejiang        hangzhou
# jiangsu          nanjing
# dtype: object
pd1.index=['金陵中学','杭州二中','石家庄一中']   #修改索引值
print(pd1)
# 金陵中学          nanjing
# 杭州二中         hangzhou
# 石家庄一中    shijiazhuang
# dtype: object
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐