【Python】Pandas中Series基本函数及举例整理

未完待续

目录

  • 未完待续
  • 介绍
  • Pandas中Series基本函数
  • 举例
    • 导入包
    • pandas.Series.shift
    • Series.str.lower 将Series中的str类型小写
    • Series.resample
    • Series.tz_localize 与自我同类型,数组/索引转换为指定的时区
    • Series.tz_convert
    • Series.to_period
    • Series.to_timestamp
    • Series.asfreq
    • Series.head

介绍

Series:一种类似于一维数组的对象,是由一组数据(各种NumPy数据类型)以及一组与之相关的数据标签(即索引)组成。仅由一组数据也可产生简单的Series对象。注意:Series中的索引值是可以重复的。
  pandas引入约定

from pandas import Series,

Pandas中Series基本函数

Series基本函数与DataFrame基本函基本一致,详细可参考DataFrame详解链接,官方文档pandas.Series链接

举例

导入包

import numpy as np
import pandas as pd

pandas.Series.shift

dates = pd.date_range('20130101', periods=6)
s = pd.Series([1, 3, 5, np.nan, 6, 8], index=dates).shift(2) # Series最后两行的值为空,为空的两行放在s的前两行

【Python】Pandas中Series基本函数及举例整理_第1张图片

Series.str.lower 将Series中的str类型小写

s = pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])
print("s.str.lower():\n",s.str.lower())

【Python】Pandas中Series基本函数及举例整理_第2张图片

Series.resample

rng = pd.date_range('1/1/2012', periods=5, freq='S')
print("rng:\n",rng)
ts = pd.Series(np.random.randint(0, 5, len(rng)), index=rng)
print("ts:\n",ts)
print("ts.resample('5Min').sum():\n",ts.resample('5Min').sum())

【Python】Pandas中Series基本函数及举例整理_第3张图片
【Python】Pandas中Series基本函数及举例整理_第4张图片
【Python】Pandas中Series基本函数及举例整理_第5张图片

Series.tz_localize 与自我同类型,数组/索引转换为指定的时区

rng = pd.date_range('3/6/2012 00:00', periods=5, freq='D')
np.random.seed(0)
ts1 = pd.Series(np.random.randn(len(rng)), rng)
print("ts1:\n",ts1)
ts1_utc = ts1.tz_localize('UTC')
print("ts1_utc:\n",ts1_utc)

【Python】Pandas中Series基本函数及举例整理_第6张图片

Series.tz_convert

print("ts1_utc.tz_convert('US/Eastern'):\n",ts1_utc.tz_convert('US/Eastern'))

【Python】Pandas中Series基本函数及举例整理_第7张图片

Series.to_period

Series.to_timestamp

rng2 = pd.date_range('1/1/2012', periods=5, freq='M')
np.random.seed(0)
ts2 = pd.Series(np.random.randn(len(rng2)), index=rng2)
print("ts2:\n",ts2)
ps = ts2.to_period()
print("ps.to_timestamp():\n",ps.to_timestamp())

【Python】Pandas中Series基本函数及举例整理_第8张图片
【Python】Pandas中Series基本函数及举例整理_第9张图片

Series.asfreq

prng = pd.period_range('1990Q1', '2000Q4', freq='Q-NOV')
print("prig:\n",prng)
ts3 = pd.Series(np.random.randn(len(prng)), prng)
print("ts3:\n",ts3)
ts3.index = (prng.asfreq('M', 'e') + 1).asfreq('H', 's') + 9
print("ts3.index:\n",ts3.index)

【Python】Pandas中Series基本函数及举例整理_第10张图片
【Python】Pandas中Series基本函数及举例整理_第11张图片
【Python】Pandas中Series基本函数及举例整理_第12张图片
【Python】Pandas中Series基本函数及举例整理_第13张图片

Series.head

print("ts3.head():\n",ts3.head())

【Python】Pandas中Series基本函数及举例整理_第14张图片

你可能感兴趣的:(python)