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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

pandas数据结构:Series/DataFrame;python函数:range/arange

發布時間:2025/7/14 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pandas数据结构:Series/DataFrame;python函数:range/arange 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?


?

1. Series

  Series 是一個類數組的數據結構,同時帶有標簽(lable)或者說索引(index)。

  1.1 下邊生成一個最簡單的Series對象,因為沒有給Series指定索引,所以此時會使用默認索引(從0到N-1)。

# 引入Series和DataFrame
In [16]: from pandas import Series,DataFrame In [17]: import pandas as pdIn [18]: ser1 = Series([1,2,3,4])In [19]: ser1 Out[19]: 0 1 1 2 2 3 3 4 dtype: int64

  1.2 當要生成一個指定索引的Series 時候,可以這樣:  

# 給index指定一個list
In [23]: ser2 = Series(range(4),index = ["a","b","c","d"])In [24]: ser2 Out[24]: a 0 b 1 c 2 d 3 dtype: int64

  1.3 也可以通過字典來創建Series對象

In [45]: sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}In [46]: ser3 = Series(sdata) # 可以發現,用字典創建的Series是按index有序的 In [47]: ser3 Out[47]: Ohio 35000 Oregon 16000 Texas 71000 Utah 5000 dtype: int64

  在用字典生成Series的時候,也可以指定索引,當索引中值對應的字典中的值不存在的時候,則此索引的值標記為Missing,NA,并且可以通過函數(pandas.isnull,pandas.notnull)來確定哪些索引對應的值是沒有的。

In [48]: states = ['California', 'Ohio', 'Oregon', 'Texas']In [49]: ser3 = Series(sdata,index = states)In [50]: ser3 Out[50]: California NaN Ohio 35000.0 Oregon 16000.0 Texas 71000.0 dtype: float64
# 判斷哪些值為空
In [51]: pd.isnull(ser3)
Out[51]:
California???? True
Ohio????????? False
Oregon??????? False
Texas???????? False
dtype: bool

In [52]: pd.notnull(ser3)
Out[52]:
California??? False
Ohio?????????? True
Oregon???????? True
Texas????????? True
dtype: bool

?

  1.4 訪問Series中的元素和索引:

# 訪問索引為"a"的元素
In [25]: ser2["a"] Out[25]: 0 # 訪問索引為"a","c"的元素 In [26]: ser2[["a","c"]] Out[26]: a 0 c 2 dtype: int64 # 獲取所有的值 In [27]: ser2.values Out[27]: array([0, 1, 2, 3]) # 獲取所有的索引 In [28]: ser2.index Out[28]: Index([u'a', u'b', u'c', u'd'], dtype='object')

  1.5 簡單運算

  在pandas的Series中,會保留NumPy的數組操作(用布爾數組過濾數據,標量乘法,以及使用數學函數),并同時保持引用的使用

In [34]: ser2[ser2 > 2] Out[34]: a 64 d 3 dtype: int64In [35]: ser2 * 2 Out[35]: a 128 b 2 c 4 d 6 dtype: int64In [36]: np.exp(ser2) Out[36]: a 6.235149e+27 b 2.718282e+00 c 7.389056e+00 d 2.008554e+01 dtype: float64

  1.6 Series的自動對齊

    Series的一個重要功能就是自動對齊(不明覺厲),看看例子就明白了。 差不多就是不同Series對象運算的時候根據其索引進行匹配計算。

# ser3 的內容
In [60]: ser3 Out[60]: Ohio 35000 Oregon 16000 Texas 71000 Utah 5000 dtype: int64 # ser4 的內容 In [61]: ser4 Out[61]: California NaN Ohio 35000.0 Oregon 16000.0 Texas 71000.0 dtype: float64 # 相同索引值的元素相加 In [62]: ser3 + ser4 Out[62]: California NaN Ohio 70000.0 Oregon 32000.0 Texas 142000.0 Utah NaN dtype: float64

  1.7 命名

  Series對象本身,以及索引都有一個 name 屬性

In [64]: ser4.index.name = "state"In [65]: ser4.name = "population"In [66]: ser4 Out[66]: state California NaN Ohio 35000.0 Oregon 16000.0 Texas 71000.0 Name: population, dtype: float64

?

轉自:http://www.cnblogs.com/linux-wangkun/p/5903380.html


?

DataFrame

用pandas中的DataFrame時選取行或列:

import numpy as np import pandas as pd from pandas import Sereis, DataFrameser = Series(np.arange(3.))data = DataFrame(np.arange(16).reshape(4,4),index=list('abcd'),columns=list('wxyz'))data['w'] #選擇表格中的'w'列,使用類字典屬性,返回的是Series類型data.w #選擇表格中的'w'列,使用點屬性,返回的是Series類型data[['w']] #選擇表格中的'w'列,返回的是DataFrame類型data[['w','z']] #選擇表格中的'w'、'z'列data[0:2] #返回第1行到第2行的所有行,前閉后開,包括前不包括后data[1:2] #返回第2行,從0計,返回的是單行,通過有前后值的索引形式,#如果采用data[1]則報錯data.ix[1:2] #返回第2行的第三種方法,返回的是DataFrame,跟data[1:2]同data['a':'b'] #利用index值進行切片,返回的是**前閉后閉**的DataFrame, #即末端是包含的 #——————新版本pandas已舍棄該方法,用iloc代替——————— data.irow(0) #取data的第一行 data.icol(0) #取data的第一列ser.iget_value(0) #選取ser序列中的第一個 ser.iget_value(-1) #選取ser序列中的最后一個,這種軸索引包含索引器的series不能采用ser[-1]去獲取最后一個,這會引起歧義。 #————————————————————————————-----------------data.head() #返回data的前幾行數據,默認為前五行,需要前十行則data.head(10) data.tail() #返回data的后幾行數據,默認為后五行,需要后十行則data.tail(10)data.iloc[-1] #選取DataFrame最后一行,返回的是Series data.iloc[-1:] #選取DataFrame最后一行,返回的是DataFramedata.loc['a',['w','x']] #返回‘a’行'w'、'x'列,這種用于選取行索引列索引已知data.iat[1,1] #選取第二行第二列,用于已知行、列位置的選取。

 轉自:https://blog.csdn.net/xiaodongxiexie/article/details/53108959


?

DataFrame的排序

原來的方法sort/sort_index都已經過時,調用時會報錯:

sort方法就直接找不到。

應該調用sort_values方法來進行排序:

?

?


Python 中的range,以及numpy包中的arange函數

range()函數

?

  • 函數說明:?range(start, stop[, step]) -> range object,根據start與stop指定的范圍以及step設定的步長,生成一個序列。
    參數含義:start:計數從start開始。默認是從0開始。例如range(5)等價于range(0, 5);
    ? ? ? ? ? ? ? end:技術到end結束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5
    ? ? ? ? ? ? ? scan:每次跳躍的間距,默認為1。例如:range(0, 5) 等價于 range(0, 5, 1)
    函數返回的是一個range object
    例子:
>>> range(0,5) #生成一個range object,而不是[0,1,2,3,4] range(0, 5) >>> c = [i for i in range(0,5)] #從0 開始到4,不包括5,默認的間隔為1 >>> c [0, 1, 2, 3, 4] >>> c = [i for i in range(0,5,2)] #間隔設為2 >>> c [0, 2, 4]

若需要生成[ 0. ? 0.1 ?0.2 ?0.3 ?0.4 ?0.5 ?0.6 ?0.7 ?0.8 ?0.9]

>>> range(0,1,0.1) #range中的setp 不能使float Traceback (most recent call last): File ”<pyshell#5>”, line 1, in <module> range(0,1,0.1) TypeError: ’float’ object cannot be interpreted as an integer

?

arrange()函數

?

  • 函數說明:arange([start,] stop[, step,], dtype=None)根據start與stop指定的范圍以及step設定的步長,生成一個?ndarray。?dtype : dtype
    ? ? ? ? The type of the output array. ?If `dtype` is not given, infer the data
    ? ? ? ? type from the other input arguments.
>>> np.arange(3) array([0, 1, 2]) >>> np.arange(3.0) array([ 0., 1., 2.]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5]) >>> arange(0,1,0.1) array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])

?

轉自:http://blog.csdn.net/qianwenhong/article/details/41414809

?

?

?

?

?

Python?中的range,以及numpy包中的arange函數

?

range()函數

?

  • 函數說明:?range(start, stop[, step]) -> range object,根據start與stop指定的范圍以及step設定的步長,生成一個序列。
    參數含義:start:計數從start開始。默認是從0開始。例如range(5)等價于range(0, 5);
    ? ? ? ? ? ? ? end:技術到end結束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5
    ? ? ? ? ? ? ? scan:每次跳躍的間距,默認為1。例如:range(0, 5) 等價于 range(0, 5, 1)
    函數返回的是一個range object
    例子:
  • [python]?view plaincopy
  • >>>?range(0,5)?????????????????#生成一個range?object,而不是[0,1,2,3,4]???
  • range(0,?5)?????
  • >>>?c?=?[i?for?i?in?range(0,5)]?????#從0?開始到4,不包括5,默認的間隔為1??
  • >>>?c??
  • [0,?1,?2,?3,?4]??
  • >>>?c?=?[i?for?i?in?range(0,5,2)]???#間隔設為2??
  • >>>?c??
  • [0,?2,?4]??
  • >>> range(0,5) #生成一個range object,而不是[0,1,2,3,4] range(0, 5) >>> c = [i for i in range(0,5)] #從0 開始到4,不包括5,默認的間隔為1 >>> c [0, 1, 2, 3, 4] >>> c = [i for i in range(0,5,2)] #間隔設為2 >>> c [0, 2, 4]
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 若需要生成[ 0. ? 0.1 ?0.2 ?0.3 ?0.4 ?0.5 ?0.6 ?0.7 ?0.8 ?0.9]
    [python]?view plaincopy
  • >>>?range(0,1,0.1)????#range中的setp?不能使float??
  • Traceback?(most?recent?call?last):??
  • ??File?”<pyshell#5>”,?line?1,?in?<module>??
  • ????range(0,1,0.1)??
  • TypeError:?’float’?object?cannot?be?interpreted?as?an?integer??
  • >>> range(0,1,0.1) #range中的setp 不能使float Traceback (most recent call last):File "<pyshell#5>", line 1, in <module>range(0,1,0.1) TypeError: 'float' object cannot be interpreted as an integer
    • 1
    • 2
    • 3
    • 4
    • 5

?

arrange()函數

?

  • 函數說明:arange([start,] stop[, step,], dtype=None)根據start與stop指定的范圍以及step設定的步長,生成一個?ndarray。?dtype : dtype
    ? ? ? ? The type of the output array. ?If `dtype` is not given, infer the data
    ? ? ? ? type from the other input arguments. [python]?view plaincopy
  • >>>?np.arange(3)??
  • ??array([0,?1,?2])??
  • ??>>>?np.arange(3.0)??
  • ??array([?0.,??1.,??2.])??
  • ??>>>?np.arange(3,7)??
  • ??array([3,?4,?5,?6])??
  • ??>>>?np.arange(3,7,2)??
  • ??array([3,?5])??
  • >>> np.arange(3)array([0, 1, 2])>>> np.arange(3.0)array([ 0., 1., 2.])>>> np.arange(3,7)array([3, 4, 5, 6])>>> np.arange(3,7,2)array([3, 5])
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • [python]?view plaincopy
  • >>>?arange(0,1,0.1)??
  • array([?0.?,??0.1,??0.2,??0.3,??0.4,??0.5,??0.6,??0.7,??0.8,??0.9])??
  • >>> arange(0,1,0.1) array([ 0. , ?0.1, ?0.2, ?0.3, ?0.4, ?0.5, ?0.6, ?0.7, ?0.8, ?0.9])

轉載于:https://www.cnblogs.com/xianhan/p/9429849.html

總結

以上是生活随笔為你收集整理的pandas数据结构:Series/DataFrame;python函数:range/arange的全部內容,希望文章能夠幫你解決所遇到的問題。

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