pandas基础(part1)--Series
生活随笔
收集整理的這篇文章主要介紹了
pandas基础(part1)--Series
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
學習筆記,這個筆記以例子為主。
開發工具:Spyder
文章目錄
- pandas介紹
- Series
- 創建Series
- 訪問Series中的數據
- pandas日期處理
- DateTimeIndex
pandas介紹
pandas是基于NumPy 的一種工具,該工具是為了解決數據分析任務而創建的。Pandas 納入 了大量庫和一些標準的數據模型,提供了高效地操作大型結構化數據集所需的工具。
Series
Series可以理解為一個一維的數組,只是index名稱可以自己改動。類似于定長的有序字典,有Index和 value。
創建Series
- 語法
- 例子
代碼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中的數據
- 語法
- 例子
代碼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: objectpandas日期處理
- 語法
- 例子
代碼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: int64Series.dt提供了很多日期相關操作, 部分操作如下:
| Series.dt.year | The year of the datetime. |
| Series.dt.month | The month as January=1, December=12. |
| Series.dt.day | The days of the datetime. |
| Series.dt.hour | The hours of the datetime. |
| Series.dt.minute | The minutes of the datetime. |
| Series.dt.second | The seconds of the datetime. |
| Series.dt.microsecond | The microseconds of the datetime. |
| Series.dt.week | The week ordinal of the year. |
| Series.dt.weekofyear | The week ordinal of the year. |
| Series.dt.dayofweek | The day of the week with Monday=0, Sunday=6. |
| Series.dt.weekday | The day of the week with Monday=0, Sunday=6. |
| Series.dt.dayofyear | The ordinal day of the year. |
| Series.dt.quarter | The quarter of the date. |
| Series.dt.is_month_start | Indicates whether the date is the first day of the month. |
| Series.dt.is_month_end | Indicates whether the date is the last day of the month. |
| Series.dt.is_quarter_start | Indicator for whether the date is the first day of a quarter. |
| Series.dt.is_quarter_end | Indicator for whether the date is the last day of a quarter. |
| Series.dt.is_year_start | Indicate whether the date is the first day of a year. |
| Series.dt.is_year_end Indicate | whether the date is the last day of the year. |
| Series.dt.is_leap_year | Boolean indicator if the date belongs to a leap year. |
| Series.dt.days_in_month | The 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: int64DateTimeIndex
通過指定周期和頻率,使用pd.date_range()函數就可以創建日期序列。
- 語法
- 例子
代碼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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 边缘世界怎么种植食物比较好?环世界种植区
- 下一篇: pandas基础(part2)--Dat