python3-pandas 数据结构 Series、DataFrame 基础
Pandas 應用
Pandas 的主要數據結構是 Series (一維數據)與 DataFrame(二維數據),這兩種數據結構足以處理金融、統計、社會科學、工程等領域里的大多數典型用例。
數據結構
Series 是一種類似于一維數組的對象,它由一組數據(各種Numpy數據類型)以及一組與之相關的數據標簽(即索引)組成。
DataFrame 是一個表格型的數據結構,它含有一組有序的列,每列可以是不同的值類型(數值、字符串、布爾型值)。DataFrame 既有行索引也有列索引,它可以被看做由 Series 組成的字典(共同用一個索引)。
1、Pandas 數據結構 - Series
Series 帶標簽的一維數組
pandas.Series( data, index, dtype, name, copy)
參數說明:
- data:一組數據(ndarray 類型)。
- index:數據索引標簽,如果不指定,默認從 0 開始。
- dtype:數據類型,默認會自己判斷。
- name:設置名稱。
- copy:拷貝數據,默認為 False。
如果沒有指定索引,索引值就從 0 開始
t = pd.Series([4,5,6]) print(t) print(type(t)) # <class 'pandas.core.series.Series'> print(t[1]) # 5 """ 0 4 1 5 2 6 dtype: int64 <class 'pandas.core.series.Series'> 5 """指定索引值,修改數據類型:
t2 = pd.Series([2,4,6,8], index=list("abcd")) print(t2) print(t2["c"]) # 6 print(t2.astype(float)) print(t2[t2>5]) """ a 2 b 4 c 6 d 8 dtype: int64 6 a 2.0 b 4.0 c 6.0 d 8.0 dtype: float64 c 6 d 8 dtype: int64 """使用 key/value 對象,類似字典來創建 Series
temp_dict = {"name": "wang1", "age": 18, "tel": 10010}t3 = pd.Series(temp_dict) print(t3) print(t3["age"]) # 18 print(t3[1]) # 18 print(t3[:2]) print(t3[[1,2]]) print(t3[["name","tel"]]) """ name wang1 age 18 tel 10010 dtype: object 18 18 name wang1 age 18 dtype: object age 18 tel 10010 dtype: object name wang1 tel 10010 dtype: object """獲取 Series 的值、索引
print(t3.index) # Index(['name', 'age', 'tel'], dtype='object') print(type(t3.index)) # <class 'pandas.core.indexes.base.Index'>print(t3.values) # ['wang1' 18 10010] print(type(t3.values)) # <class 'numpy.ndarray'>2、Pandas 數據結構 - DataFrame
DataFrame 是一個表格型的數據結構,它含有一組有序的列,每列可以是不同的值類型(數值、字符串、布爾型值)。DataFrame 既有行索引也有列索引,它可以被看做由 Series 組成的字典(共同用一個索引)
DataFrame 構造方法如下:
pandas.DataFrame( data, index, columns, dtype, copy)
DataFrame 二維,Series 容器
參數說明:
- data:一組數據(ndarray、series, map, lists, dict 等類型)。
- index:索引值,或者可以稱為行標簽。
- columns:列標簽,默認為 RangeIndex (0, 1, 2, …, n) 。
- dtype:數據類型。
- copy:拷貝數據,默認為 False。
DataFrame對象既有行索引,又有列索引
行索引,表明不同行,橫向索引,叫index,0軸,axis=0
列索引,表明不同列,縱向索引,叫columns,1軸,axis=1
2.1、index、columns 使用:
t1 = pd.DataFrame(np.arange(12).reshape(3,4), index=list("abc"), columns=list("wxyz")) print(t1) """w x y z a 0 1 2 3 b 4 5 6 7 c 8 9 10 11 """2.2、使用列表創建DataFrame,缺失的值用 NaN 代替
data = [['Google',10],['Runoob',12],['Wiki',13]] df = pd.DataFrame(data,columns=['Site','Age']) print(df) """Site Age 0 Google 10 1 Runoob 12 2 Wiki 13 """2.3、使用字典創建DataFrame,缺失的值用 NaN 代替
data = {'Site':['Google', 'Runoob', 'Wiki'], 'Age':[10, 12, 13]} df = pd.DataFrame(data) print (df) """Site Age 0 Google 10 1 Runoob 12 2 Wiki 13 """ data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] df = pd.DataFrame(data) print (df) """a b c 0 1 2 NaN 1 5 10 20.0 """2.4、DataFrame基礎屬性
DataFrame.shape # 行數 列數 DataFrame.dtypes # 列數據類型 DataFrame.ndim # 數據維度 DataFrame.index # 行索引 DataFrame.columns # 列索引 DataFrame.values # 對象值DataFrame.head(3) # 顯示頭部幾行,默認5行 DataFrame.tail(3) # 顯示末尾幾行,默認5行 DataFrame.info() # 相關信息概覽:行數,列數,列索引,列非空值個數,列類型,內存占用 DataFrame.describe() # 快速綜合統計結果:計數,均值,標準差,最大值,四分位數,最小值 data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] df = pd.DataFrame(data) print(df) """a b c 0 1 2 NaN 1 5 10 20.0 """ print(df.index) # RangeIndex(start=0, stop=2, step=1) print(df.columns) # Index(['a', 'b', 'c'], dtype='object') print(df.values) # [[ 1. 2. nan] [ 5. 10. 20.]] print(df.shape) # (2, 3) print(df.ndim) # 數據維度 2 print(df.dtypes) # 列數據類型 """ a int64 b int64 c float64 dtype: object """ print("*"*80) print(df.info()) """ <class 'pandas.core.frame.DataFrame'> RangeIndex: 2 entries, 0 to 1 Data columns (total 3 columns):# Column Non-Null Count Dtype --- ------ -------------- ----- 0 a 2 non-null int64 1 b 2 non-null int64 2 c 1 non-null float64 dtypes: float64(1), int64(2) memory usage: 176.0 bytes None """ print(df.describe()) """a b c count 2.000000 2.000000 1.0 mean 3.000000 6.000000 20.0 std 2.828427 5.656854 NaN min 1.000000 2.000000 20.0 25% 2.000000 4.000000 20.0 50% 3.000000 6.000000 20.0 75% 4.000000 8.000000 20.0 max 5.000000 10.000000 20.0 """2.5、DataFrame 排序
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}] df = pd.DataFrame(data) print(df) """a b c 0 1 2 NaN 1 5 10 20.0 """ # ascending=True 升序 # ascending=False 降序 df = df.sort_values("c", ascending=False) print(df) """a b c 1 5 10 20.0 0 1 2 NaN """https://www.runoob.com/pandas/pandas-series.html
https://www.bilibili.com/video/BV1hx411d7jb?p=23
https://www.bilibili.com/video/BV1hx411d7jb?p=24
https://www.bilibili.com/video/BV1hx411d7jb?p=25
https://www.bilibili.com/video/BV1hx411d7jb?p=26
總結
以上是生活随笔為你收集整理的python3-pandas 数据结构 Series、DataFrame 基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iscroll的使用
- 下一篇: python获取视频时长方法