python中pandas.DataFrame的簡(jiǎn)單操作方法(創(chuàng)建、索引、增添與刪除)
前言
最近在網(wǎng)上搜了許多關(guān)于pandas.DataFrame
的操作說(shuō)明,都是一些基礎(chǔ)的操作,但是這些操作組合起來(lái)還是比較費(fèi)時(shí)間去正確操作DataFrame,花了我挺長(zhǎng)時(shí)間去調(diào)整BUG的。我在這里做一些總結(jié),方便你我他。感興趣的朋友們一起來(lái)看看吧。
一、創(chuàng)建DataFrame的簡(jiǎn)單操作:
1、根據(jù)字典創(chuàng)造:
In [1]: import pandas as pd In [3]: aa={'one':[1,2,3],'two':[2,3,4],'three':[3,4,5]} In [4]: bb=pd.DataFrame(aa) In [5]: bb Out[5]: one three two 0 1 3 2 1 2 4 3 2 3 5 4`
字典中的keys就是DataFrame里面的columns,但是沒(méi)有index的值,所以需要自己設(shè)定,不設(shè)定默認(rèn)是從零開(kāi)始計(jì)數(shù)。
bb=pd.DataFrame(aa,index=['first','second','third']) bb Out[7]: one three two first 1 3 2 second 2 4 3 third 3 5 4
2、從多維數(shù)組中創(chuàng)建
import numpy as np In [9]: del aa In [10]: aa=np.array([[1,2,3],[4,5,6],[7,8,9]]) In [11]: aa Out[11]: array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) In [12]: bb=pd.DataFrame(aa) In [13]: bb Out[13]: 0 1 2 0 1 2 3 1 4 5 6 2 7 8 9
從多維數(shù)組中創(chuàng)建就需要為DataFrame賦值columns和index,否則就是默認(rèn)的,很丑的。
bb=pd.DataFrame(aa,index=[22,33,44],columns=['one','two','three']) In [15]: bb Out[15]: one two three 22 1 2 3 33 4 5 6 44 7 8 9
3、用其他的DataFrame創(chuàng)建
bb=pd.DataFrame(aa,index=[22,33,44],columns=['one','two','three']) bb Out[15]: one two three 22 1 2 3 33 4 5 6 44 7 8 9 cc=bb[['one','three']].copy() Cc Out[17]: one three 22 1 3 33 4 6 44 7 9
這里的拷貝是深拷貝,改變cc中的值并不能改變bb中的值。
cc['three'][22]=5 bb Out[19]: one two three 22 1 2 3 33 4 5 6 44 7 8 9 cc Out[20]: one three 22 1 5 33 4 6 44 7 9
二、DataFrame的索引操作:
對(duì)于一個(gè)DataFrame來(lái)說(shuō),索引是最煩的,最易出錯(cuò)的。
1、索引一列或幾列,比較簡(jiǎn)單:
bb['one'] Out[21]: 22 1 33 4 44 7 Name: one, dtype: int32
多個(gè)列名需要將輸入的列名存在一個(gè)列表里,才是個(gè)collerable的變量,否則會(huì)報(bào)錯(cuò)。
bb[['one','three']] Out[29]: one three 22 1 3 33 4 6 44 7 9
2、索引一條記錄或幾條記錄:
bb[1:3] Out[27]: one two three 33 4 5 6 44 7 8 9 bb[:1] Out[28]: one two three 22 1 2 3
這里注意冒號(hào)是必須有的,否則是索引列的了。
3、索引某幾列的變量的某幾條記錄,這個(gè)折磨了我好久:
第一種
bb.loc[[22,33]][['one','three']] Out[30]: one three 22 1 3 33 4 6
這種不能改變這里面的值,你只是能讀值,不能寫(xiě)值,可能和loc()
函數(shù)有關(guān):
bb.loc[[22,33]][['one','three']]=[[2,2],[3,6]] In [32]: bb Out[32]: one two three 22 1 2 3 33 4 5 6 44 7 8 9
第二種:也是只能看
bb[['one','three']][:2] Out[33]: one three 22 1 3 33 4 6
想要改變其中的值就會(huì)報(bào)錯(cuò)。
In [34]: bb[['one','three']][:2]=[[2,2],[2,2]] -c:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_index,col_indexer] = value instead F:\Anaconda\lib\site-packages\pandas\core\frame.py:1999: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame return self._setitem_slice(indexer, value)
第三種:可以改變數(shù)據(jù)的值?。。?/p>
Iloc是按照數(shù)據(jù)的行列數(shù)來(lái)索引,不算index和columns
bb.iloc[2:3,2:3] Out[36]: three 44 9 bb.iloc[1:3,1:3] Out[37]: two three 33 5 6 44 8 9 bb.iloc[0,0] Out[38]: 1
下面是證明:
bb.iloc[0:4,0:2]=[[9,9],[9,9],[9,9]] In [45]: bb Out[45]: one two three 22 9 9 3 33 9 9 6 44 9 9 9
三、在原有的DataFrame上新建一個(gè)columns或幾個(gè)columns
1、什么都不用的,只能單獨(dú)創(chuàng)建一列,多列并不好使,親測(cè)無(wú)效:
bb['new']=[2,3,4] bb Out[51]: one two three new 22 9 9 3 2 33 9 9 6 3 44 9 9 9 4 bb[['new','new2']]=[[2,3,4],[5,3,7]] KeyError: "['new' 'new2'] not in index"
賦予的list基本就是按照所給index值順序賦值,可是一般我們是要對(duì)應(yīng)的index進(jìn)行賦值,想要更高級(jí)的賦值就看后面的了。
2、使用字典進(jìn)行多列按index賦值:
aa={33:[234,44,55],44:[657,77,77],22:[33,55,457]} In [58]: bb=bb.join(pd.DataFrame(aa.values(),columns=['hi','hello','ok'],index=aa.keys())) In [59]: bb Out[59]: one two three new hi hello ok 22 9 9 3 2 33 55 457 33 9 9 6 3 234 44 55 44 9 9 9 4 657 77 77
這里aa是一個(gè)字典和列表的嵌套,相當(dāng)于一條記錄,使用keys當(dāng)做index名而不是一般默認(rèn)的columns名。達(dá)到了按index多列匹配的目的。由于dict()
儲(chǔ)存是混亂的,之間用dict()
而不給他的index賦值會(huì)記錄錯(cuò)亂,這一點(diǎn)注意值得注意。
四、刪除多列或多記錄:
刪除列
bb.drop(['new','hi'],axis=1) Out[60]: one two three hello ok 22 9 9 3 55 457 33 9 9 6 44 55 44 9 9 9 77 77
刪除記錄
bb.drop([22,33],axis=0) Out[61]: one two three new hi hello ok 44 9 9 9 4 657 77 77
跟大家分享一篇關(guān)于python中pandas.DataFrame對(duì)行與列求和及添加新行與列示例,感興趣的朋友們可以看看。
DataFrame還有很多功能還沒(méi)有涉及,等以后有涉及到,看完官網(wǎng)的API之后,還會(huì)繼續(xù)分享,everything is ok。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Python中循環(huán)后使用list.append()數(shù)據(jù)被覆蓋問(wèn)題的解決
這篇文章主要給大家介紹了關(guān)于Python中循環(huán)后使用list.append()數(shù)據(jù)被覆蓋問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07python實(shí)現(xiàn)上傳樣本到virustotal并查詢(xún)掃描信息的方法
這篇文章主要介紹了python實(shí)現(xiàn)上傳樣本到virustotal并查詢(xún)掃描信息的方法,是比較實(shí)用的技巧,需要的朋友可以參考下2014-10-10python實(shí)現(xiàn)簡(jiǎn)單的TCP代理服務(wù)器
這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單的TCP代理服務(wù)器,包含了完整的實(shí)現(xiàn)過(guò)程及對(duì)應(yīng)的源碼與說(shuō)明文檔下載,非常具有參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10python抽樣方法解讀及實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了python抽樣方法解讀及實(shí)現(xiàn)過(guò)程講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02python3+PyQt5圖形項(xiàng)的自定義和交互 python3實(shí)現(xiàn)page Designer應(yīng)用程序
這篇文章主要為大家詳細(xì)介紹了python3+PyQt5圖形項(xiàng)的自定義和交互,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04