关于Pandas是干嘛的,功能有多强大我就不多说看了,我直接上干货。
1、导入pandas包
import pandas as pd
2、创建一个Series序列
pd.Series([1, 3, 5, np.nan, np.nan, 6, 8])
print(s)
0 1.0
1 3.0
2 5.0
3 NaN
4 6.0
5 8.0
dtype: float64
3、根据日期创建一个序列(DatetimeIndex)
periods 表示的时期 freq=“D” 表示的是以 Day 增加也就是天数目,所以一直是日在增加,当然也可以改成“M”,具体可以参考 freq参数可选项目 还有一些参数,比如开始、结束、时区等相关参数。
4 、设置index的Series
s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])
a 0.114858
b 0.872704
c 0.284864
d 0.884343
e 0.249049
dtype: float64
5、从字典里生成Series
d = {"a": 0.0, "b": 1.0, "c": 2.0}
pd.Series(d)
6、Series切片
data2 = pd.Series(np.random.randint(1, 5, 5), index=["a", "b", "c", "d", "e"], dtype="uint32")
print(data2)
print("-" * 32)
print("median", data2.median())
print("-" * 32)
print(data2[:2])
print("-" * 32)
print(data2[data2 > data2.median()])
print(data2[[4, 3, 1]])
print(np.exp(data2))
a 1
b 3
c 2
d 4
e 2
dtype: uint32
--------------------------------
median 2.0
--------------------------------
a 1
b 3
dtype: uint32
--------------------------------
b 3
d 4
dtype: uint32
e 2
d 4
b 3
dtype: uint32
a 2.718282
b 20.085537
c 7.389056
d 54.598150
e 7.389056
dtype: float64
Process finished with exit code 0
6、Series类似字典的操作
data2 = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"], dtype="uint32")
print(data2['a'])
print('b' in data2)
print(data2.get("d"))
7、Series类似 narray的操作
data2 = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"], dtype="uint32")
print(data2+data2)
print(2*data2)
print(np.exp(data2))
8、Series 设置name和修改
data2 = pd.Series([1, 2, 3, 4, 5], index=["a", "b", "c", "d", "e"], dtype="uint32")
data2.name = "hongbiao"
print(data2.name)
data = data2.rename("zhangsan")
print(data.name)
好了,上面就是Series的一些基本知识,当然还有Series的一些索引,我这里就不多说了,后面在索引的时候再说
|