Python資料處理工具使用方法整理

來源:互聯網
上載者:User

標籤:group   dataframe   mode   python   read   擷取   索引   ESS   使用   

# 從csv檔案建立DataFrame類型的資料結構>>>df=pd.read_csv("xxx.csv")# DataFrame類型的形狀和長度>>> df.shape(38, 39)>>> len(df)38# 各列的標題和資料類型>>> df.columns>>> df.dtypes# 索引>>> df.indexRangeIndex(start=0, stop=38, step=1)# 將DataFrame轉化成Numpy數組>>>df.values# 查看變數類型>>> type(df)<class ‘pandas.core.frame.DataFrame‘># 擷取DataFrame的一列(得到的資料類型是Series)>>> type(df)<class ‘pandas.core.frame.DataFrame‘>>>> col=df[‘104‘]>>> type(col)<class ‘pandas.core.series.Series‘># Series中與DataFrame相似的屬性>>> col.shape(38,)>>> col.valuesarray([301,  1051,  1657,  1852,  2057,  2258,  2938,  3418, 3718,  3938,  4148,  4568,  5068])>>> col.indexRangeIndex(start=0, stop=38, step=1)>>> col.name‘104‘# 截取最後幾個元素>>> col[-2:]36    6553637    65536Name: 104, dtype: int64>>> type(col[-2:])<class ‘pandas.core.series.Series‘># DataFrame的符號>>> np.sign(df)>>> last_col=df.columns[-1]>>> np.sign(df[last_col])# head(取前幾行)和tail(取後幾行)>>> df.head(2)>>> df.tail(2)# 按索引尋找某一行資料>>> last_col=df.index[-1]>>> last_col>>> df.iloc[last_col]# 按索引尋找某一行的某一列資料>>> df.iloc[2:9]# iloc和iat作用相同>>> df.iloc[2,3]>>> df.iat[2,3]# 邏輯尋找>>> df[df>df.mean()]# 統計計算# 描述資訊>>> df.describe()# 非空資料的數量>>> df.count()# 平均絕對偏差(類似於標準差)>>> df.mad()# 中位元>>> df.median()# 最小值>>> df.min()# 最大值>>> df.max()# 眾數>>> df.mode()# 標準差>>> df.std()# 方差>>> df.var()# 偏態係數(skewness,表示資料的對稱程度)>>> df.skew()# 峰態函數(kurtosis,表示資料分布圖的尖扁程度)>>> df.kurt()# 用python字典產生DataFrame>>> df=pd.DataFrame({‘weather‘:[‘cold‘,‘hot‘],‘food‘:[‘soup‘,‘ice cream‘]})>>> df        food weather0       soup    cold1  ice cream     hot# 對某個屬性按類型分組>>> group=df.groupby(‘weather‘)>>> for name,gro in group:...     print(name)...     print(gro)... cold   food weather0  soup    cold2  cake    coldhot        food weather1  ice cream     hot3      bread     hot>>> group<pandas.core.groupby.groupby.DataFrameGroupBy object at 0x7f110c24d1d0># 各分組第一行、最後一行、平均數>>> group=df.groupby(‘weather‘)>>> group.first()              food  priceweather                  cold          soup      1hot      ice cream      2>>> group.last()          food  priceweather              cold      cake      3hot      bread      4>>> group.mean()         priceweather       cold         2hot          3# 查看分組>>> g=df.groupby([‘weather‘,‘food‘])>>> g.groups{(‘hot‘, ‘bread‘): Int64Index([3], dtype=‘int64‘), (‘cold‘, ‘cake‘): Int64Index([2], dtype=‘int64‘), (‘hot‘, ‘ice cream‘): Int64Index([1], dtype=‘int64‘), (‘cold‘, ‘soup‘): Int64Index([0], dtype=‘int64‘)}# 為分組追加屬性>>> g.agg([np.mean])                  price                   meanweather food           cold    cake          3        soup          1hot     bread         4        ice cream     2# 截取幾行資料並串連>>> d=pd.concat([df[:2],df[3:]])>>> d>>> d=pd.concat([df[:2],df[3:]])>>> d        food  price weather0       soup      1    cold1  ice cream      2     hot3      bread      4     hot>>> d.append(df[3:])        food  price weather0       soup      1    cold1  ice cream      2     hot3      bread      4     hot3      bread      4     hot

 

Python資料處理工具使用方法整理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.