Pandas中DataFrame常用操作指南
前言
Pandas是Python下一個(gè)開源數(shù)據(jù)分析的庫(kù),它提供的數(shù)據(jù)結(jié)構(gòu)DataFrame極大的簡(jiǎn)化了數(shù)據(jù)分析過(guò)程中一些繁瑣操作。
1. 基本使用:
創(chuàng)建DataFrame. DataFrame是一張二維的表,大家可以把它想象成一張Excel表單或者Sql表。
Excel 2007及其以后的版本的最大行數(shù)是1048576,最大列數(shù)是16384,超過(guò)這個(gè)規(guī)模的數(shù)據(jù)Excel就會(huì)彈出個(gè)框框“此文本包含多行文本,無(wú)法放置在一個(gè)工作表中”。
Pandas處理上千萬(wàn)的數(shù)據(jù)是易如反掌的sh事情,同時(shí)隨后我們也將看到它比SQL有更強(qiáng)的表達(dá)能力,可以做很多復(fù)雜的操作,要寫的code也更少。 說(shuō)了一大堆它的好處,要實(shí)際感觸還得動(dòng)手碼代碼。
首要的任務(wù)就是創(chuàng)建一個(gè)DataFrame,它有幾種創(chuàng)建方式:
- 列表,序列(pandas.Series), numpy.ndarray的字典
- 二維numpy.ndarray
- 別的DataFrame
- 結(jié)構(gòu)化的記錄(structured arrays)
其中,我最喜歡的是通過(guò)二維ndarray創(chuàng)建DataFrame,因?yàn)榇a敲得最少:
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn( 3 , 4 )) df 0 1 2 3 0 0.236175 - 0.394792 - 0.171866 0.304012 1 0.651926 0.989046 0.160389 0.482936 2 - 1.039824 0.401105 - 0.492714 - 1.220438
當(dāng)然你還可以從mysql數(shù)據(jù)庫(kù)或者csv文件中載入數(shù)據(jù)到dataframe。
dataframe中index用來(lái)標(biāo)識(shí)行,column標(biāo)識(shí)列,shape表示維度。
# 獲得行索引信息 df.index # 獲得列索引信息 df.columns # 獲得df的size df.shape # 獲得df的行數(shù) df.shape[0] # 獲得df的 列數(shù) df.shape[1] # 獲得df中的值 df.values
通過(guò)describe方法,我們可以對(duì)df中的數(shù)據(jù)有個(gè)大概的了解:
df.describe() 0 1 2 3 count 3.000000 3.000000 3.000000 3.000000 mean - 0.050574 0.331786 - 0.168064 - 0.144496 std 0.881574 0.694518 0.326568 0.936077 min - 1.039824 - 0.394792 - 0.492714 - 1.220438 25 % - 0.401824 0.003156 - 0.332290 - 0.458213 50 % 0.236175 0.401105 - 0.171866 0.304012 75 % 0.444051 0.695076 - 0.005739 0.393474 max 0.651926 0.989046 0.160389 0.482936
2. 數(shù)據(jù)select, del, update。
按照列名select:
df[ 0 ] 0 0.236175 1 0.651926 2 - 1.039824
按照行數(shù)select:
df[: 3 ] #選取前3行
按照索引select:
df.loc[ 0 ] 0 0.236175 1 - 0.394792 2 - 0.171866 3 0.304012
按照行數(shù)和列數(shù)select:
df.iloc[ 3 ] #選取第3行 df.iloc[ 2 : 4 ] #選取第2到第3行 df.iloc[ 0 , 1 ] #選取第0行1列的元素 dat.iloc[: 2 , : 3 ] #選取第0行到第1行,第0列到第2列區(qū)域內(nèi)的元素 df1.iloc[[1,3,5],[1,3]] #選取第1,3,5行,第1,3列區(qū)域內(nèi)的元素
刪除某列:
del df[0] df 1 2 3 0 - 0.394792 - 0.171866 0.304012 1 0.989046 0.160389 0.482936 2 0.401105 - 0.492714 - 1.220438
刪除某行:
5 df.drop(0) 1 2 3 1 0.989046 0.160389 0.482936 2 0.401105 - 0.492714 - 1.220438
3.運(yùn)算。
基本運(yùn)算:
df[ 4 ] = df[ 1 ] + df[ 2 ] 1 2 3 4 0 - 0.394792 - 0.171866 0.304012 - 0.566659 1 0.989046 0.160389 0.482936 1.149435 2 0.401105 - 0.492714 - 1.220438 - 0.091609
map運(yùn)算,和python中的map有些類似:
df[ 4 ]. map ( int ) 0 0 1 1 2 0
apply運(yùn)算:
df. apply ( sum ) 1 0.995359 2 - 0.504192 3 - 0.433489 4 0.491167
4. Group by 操作。
pandas中的group by 操作是我的最愛,不用把數(shù)據(jù)導(dǎo)入excel或者mysql就可以進(jìn)行靈活的group by 操作,簡(jiǎn)化了分析過(guò)程。
df[ 0 ] = [ 'A' , 'A' , 'B' ] df 1 2 3 4 0 0 - 0.394792 - 0.171866 0.304012 - 0.566659 A 1 0.989046 0.160389 0.482936 1.149435 A 2 0.401105 - 0.492714 - 1.220438 - 0.091609 B g = df.groupby([ 0 ]) g.size() A 2 B 1 g. sum () 1 2 3 4 0 A 0.594254 - 0.011478 0.786948 0.582776 B 0.401105 - 0.492714 - 1.220438 - 0.091609
5. 導(dǎo)出到csv文件
dataframe可以使用to_csv方法方便地導(dǎo)出到csv文件中,如果數(shù)據(jù)中含有中文,一般encoding指定為”utf-8″,否則導(dǎo)出時(shí)程序會(huì)因?yàn)椴荒茏R(shí)別相應(yīng)的字符串而拋出異常,index指定為False表示不用導(dǎo)出dataframe的index數(shù)據(jù)。
df.to_csv(file_path, encoding='utf-8', index=False) df.to_csv(file_path, index=False)
總結(jié)
到此這篇關(guān)于Pandas中DataFrame操作的文章就介紹到這了,更多相關(guān)Pandas DataFrame操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- pandas按行按列遍歷Dataframe的幾種方式
- Pandas DataFrame數(shù)據(jù)的更改、插入新增的列和行的方法
- pandas DataFrame的修改方法(值、列、索引)
- pandas DataFrame 行列索引及值的獲取的方法
- pandas DataFrame 刪除重復(fù)的行的實(shí)現(xiàn)方法
- 用pandas中的DataFrame時(shí)選取行或列的方法
- Pandas將列表(List)轉(zhuǎn)換為數(shù)據(jù)框(Dataframe)
- 利用Pandas 創(chuàng)建空的DataFrame方法
- Python中pandas dataframe刪除一行或一列:drop函數(shù)詳解
- python pandas dataframe 按列或者按行合并的方法
相關(guān)文章
Python functools.lru_cache裝飾器性能提升利器深入探究
本文將詳細(xì)介紹functools.lru_cache裝飾器的原理、用法以及適當(dāng)?shù)膱?chǎng)景,以幫助你更好地利用這一功能,它可以用來(lái)緩存函數(shù)的輸出,以避免重復(fù)計(jì)算,從而顯著提高程序的執(zhí)行速度2024-01-01