日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pandas基础(part1)--Series

發布時間:2023/12/19 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pandas基础(part1)--Series 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

學習筆記,這個筆記以例子為主。
開發工具:Spyder


文章目錄

    • pandas介紹
    • Series
      • 創建Series
      • 訪問Series中的數據
    • pandas日期處理
      • DateTimeIndex


pandas介紹

pandas是基于NumPy 的一種工具,該工具是為了解決數據分析任務而創建的。Pandas 納入 了大量庫和一些標準的數據模型,提供了高效地操作大型結構化數據集所需的工具。

Series

Series可以理解為一個一維的數組,只是index名稱可以自己改動。類似于定長的有序字典,有Index和 value。

創建Series

  • 語法
import pandas as pd# 創建一個空的系列 s = pd.Series() # 從ndarray創建一個系列 data = np.array(['a','b','c','d']) s = pd.Series(data) s = pd.Series(data,index=[100,101,102,103]) # 從字典創建一個系列 data = {'a' : 0., 'b' : 1., 'c' : 2.} s = pd.Series(data) # 從標量創建一個系列 s = pd.Series(5, index=[0, 1, 2, 3])
  • 例子

代碼1(從ndarray創建一個系列):

import numpy as np import pandas as pddata = np.array(['Ada', 'Bunny', 'Jack', 'Black'])s1 = pd.Series(data) print(s1)

結果1:

0 Ada 1 Bunny 2 Jack 3 Black dtype: object

代碼2(自定義index):

s2 = pd.Series(data, index = [10, 20, 30, 40]) print(s2)

結果2:

10 Ada 20 Bunny 30 Jack 40 Black dtype: object

代碼3(從字典創建一個系列):

data = {"a":0, "b":1, "c":2, 'e':3} #字典的key為Series的index s3 = pd.Series(data) print(s3)

結果3:

a 0 b 1 c 2 e 3 dtype: int64

代碼4(從標量創建一個系列):

s4 = pd.Series(10, index = [0, 1, 2, 3]) print(s4)

結果4:

0 10 1 10 2 10 3 10 dtype: int64

訪問Series中的數據

  • 語法
# 使用索引檢索元素 s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) print(s[0], s[:3], s[-3:]) # 使用標簽檢索數據 print(s['a'], s[['a','c','d']])
  • 例子

代碼1:

import numpy as np import pandas as pddata = np.array(['Ada', 'Bunny', 'Jack', 'Black'])s = pd.Series(data, index = ["a", "b", "c", "d"]) print(s[0], '\n\n',s[:3],'\n\n', s[-3: ])

結果1:

Ada a Ada b Bunny c Jack dtype: object b Bunny c Jack d Black dtype: object

代碼2:

print(s["a"], '\n\n',s[["a", "b", "c"]])

結果2:

Ada a Ada b Bunny c Jack dtype: object

pandas日期處理

  • 語法
# pandas可以識別的日期字符串格式 dates = pd.Series(['2011', '2011-02', '2011-03-01', '2011/04/01', '2011/05/01 01:01:01', '01 Jun 2011']) # to_datetime()方法可以轉換為日期數據類型 dates = pd.to_datetime(dates)
  • 例子

代碼1(識別日期):

import numpy as np import pandas as pddates = pd.Series(['1997', '2015-09', '2019-03-01','2019/04/01', '2019/05/01 01:01:01','01 Jun 2019'])print(dates) print("-"*20) dates = pd.to_datetime(dates) print(dates)

結果1:

0 1997 1 2015-09 2 2019-03-01 3 2019/04/01 4 2019/05/01 01:01:01 5 01 Jun 2019 dtype: object -------------------- 0 1997-01-01 00:00:00 1 2015-09-01 00:00:00 2 2019-03-01 00:00:00 3 2019-04-01 00:00:00 4 2019-05-01 01:01:01 5 2019-06-01 00:00:00 dtype: datetime64[ns]

代碼2(日期運算):

delta = dates - pd.to_datetime('1970-01-01') print(delta) print("-"*20) #通過Series的dt接口,可以訪問偏移量數據 print(delta.dt.days)

結果2:

0 9862 days 00:00:00 1 16679 days 00:00:00 2 17956 days 00:00:00 3 17987 days 00:00:00 4 18017 days 01:01:01 5 18048 days 00:00:00 dtype: timedelta64[ns] -------------------- 0 9862 1 16679 2 17956 3 17987 4 18017 5 18048 dtype: int64

Series.dt提供了很多日期相關操作, 部分操作如下:

Series.dt的日期相關操作含義
Series.dt.yearThe year of the datetime.
Series.dt.monthThe month as January=1, December=12.
Series.dt.dayThe days of the datetime.
Series.dt.hourThe hours of the datetime.
Series.dt.minuteThe minutes of the datetime.
Series.dt.secondThe seconds of the datetime.
Series.dt.microsecondThe microseconds of the datetime.
Series.dt.weekThe week ordinal of the year.
Series.dt.weekofyearThe week ordinal of the year.
Series.dt.dayofweekThe day of the week with Monday=0, Sunday=6.
Series.dt.weekdayThe day of the week with Monday=0, Sunday=6.
Series.dt.dayofyearThe ordinal day of the year.
Series.dt.quarterThe quarter of the date.
Series.dt.is_month_startIndicates whether the date is the first day of the month.
Series.dt.is_month_endIndicates whether the date is the last day of the month.
Series.dt.is_quarter_startIndicator for whether the date is the first day of a quarter.
Series.dt.is_quarter_endIndicator for whether the date is the last day of a quarter.
Series.dt.is_year_startIndicate whether the date is the first day of a year.
Series.dt.is_year_end Indicatewhether the date is the last day of the year.
Series.dt.is_leap_yearBoolean indicator if the date belongs to a leap year.
Series.dt.days_in_monthThe number of days in the month.

代碼3(dt接口的各項操作演示):

print(dates.dt.month)

結果3:

0 1 1 9 2 3 3 4 4 5 5 6 dtype: int64

DateTimeIndex

通過指定周期和頻率,使用pd.date_range()函數就可以創建日期序列。

  • 語法
import pandas as pd # 以日為頻率(默認值), 2019/08/21為起始,創建5個時間數據 datelist = pd.date_range('2019/08/21', periods = 5)# 以月為頻率 datelist = pd.date_range('2019/08/21', periods=5,freq='M')# 構建某個區間的時間序列 start = pd.datetime(2017, 11, 1) end = pd.datetime(2017, 11, 5) dates = pd.date_range(start, end)
  • 例子

代碼1:

import numpy as np import pandas as pddates1 = pd.date_range('2020-01-01', periods = 5,freq = 'D') print(dates1)print("-"*20)dates2 = pd.date_range('2015-01-10', periods = 5,freq = 'M') print(dates2)print("-"*20)start_num = pd.datetime(2019, 1, 1) end_num = pd.datetime(2019, 1, 5) dates3 = pd.date_range(start_num, end_num) print(dates3)

結果1:

DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04','2020-01-05'],dtype='datetime64[ns]', freq='D') -------------------- DatetimeIndex(['2015-01-31', '2015-02-28', '2015-03-31', '2015-04-30','2015-05-31'],dtype='datetime64[ns]', freq='M') -------------------- DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04','2019-01-05'],dtype='datetime64[ns]', freq='D')

代碼2:

dates1 = pd.bdate_range('2020-01-01', periods = 10) print(dates1)

備注:bdate_range()用來表示商業日期范圍,不同于date_range(),它不包括星期六和星期天。

結果2:

DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-06','2020-01-07', '2020-01-08', '2020-01-09', '2020-01-10','2020-01-13', '2020-01-14'],dtype='datetime64[ns]', freq='B')

總結

以上是生活随笔為你收集整理的pandas基础(part1)--Series的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。