前言
最近在網上搜了許多關于pandas.DataFrame的操作說明,都是一些基礎的操作,但是這些操作組合起來還是比較費時間去正確操作DataFrame,花了我挺長時間去調整BUG的。我在這里做一些總結,方便你我他。感興趣的朋友們一起來看看吧。
一、創建DataFrame的簡單操作:
1、根據字典創造:
In [1]: import pandas as pdIn [3]: aa={'one':[1,2,3],'two':[2,3,4],'three':[3,4,5]}In [4]: bb=pd.DataFrame(aa)In [5]: bbOut[5]: one three two0 1 3 21 2 4 32 3 5 4`字典中的keys就是DataFrame里面的columns,但是沒有index的值,所以需要自己設定,不設定默認是從零開始計數。
bb=pd.DataFrame(aa,index=['first','second','third'])bbOut[7]: one three twofirst 1 3 2second 2 4 3third 3 5 4
2、從多維數組中創建
import numpy as npIn [9]: del aaIn [10]: aa=np.array([[1,2,3],[4,5,6],[7,8,9]])In [11]: aaOut[11]: array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])In [12]: bb=pd.DataFrame(aa)In [13]: bbOut[13]: 0 1 20 1 2 31 4 5 62 7 8 9
從多維數組中創建就需要為DataFrame賦值columns和index,否則就是默認的,很丑的。
bb=pd.DataFrame(aa,index=[22,33,44],columns=['one','two','three'])In [15]: bbOut[15]: one two three22 1 2 333 4 5 644 7 8 9
3、用其他的DataFrame創建
bb=pd.DataFrame(aa,index=[22,33,44],columns=['one','two','three'])bbOut[15]: one two three22 1 2 333 4 5 644 7 8 9cc=bb[['one','three']].copy()CcOut[17]: one three22 1 333 4 644 7 9
這里的拷貝是深拷貝,改變cc中的值并不能改變bb中的值。
cc['three'][22]=5bbOut[19]: one two three22 1 2 333 4 5 644 7 8 9ccOut[20]: one three22 1 533 4 644 7 9
二、DataFrame的索引操作:
對于一個DataFrame來說,索引是最煩的,最易出錯的。
1、索引一列或幾列,比較簡單:
bb['one']Out[21]: 22 133 444 7Name: one, dtype: int32
多個列名需要將輸入的列名存在一個列表里,才是個collerable的變量,否則會報錯。
bb[['one','three']]Out[29]: one three22 1 333 4 644 7 9
2、索引一條記錄或幾條記錄:
bb[1:3]Out[27]: one two three33 4 5 644 7 8 9bb[:1]Out[28]: one two three22 1 2 3
這里注意冒號是必須有的,否則是索引列的了。
3、索引某幾列的變量的某幾條記錄,這個折磨了我好久:
第一種
bb.loc[[22,33]][['one','three']]Out[30]: one three22 1 333 4 6
這種不能改變這里面的值,你只是能讀值,不能寫值,可能和loc()函數有關:
bb.loc[[22,33]][['one','three']]=[[2,2],[3,6]]In [32]: bbOut[32]: one two three22 1 2 333 4 5 644 7 8 9
第二種:也是只能看
bb[['one','three']][:2]Out[33]: one three22 1 333 4 6
想要改變其中的值就會報錯。
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 insteadF:/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)
第三種:可以改變數據的值!!!
Iloc是按照數據的行列數來索引,不算index和columns
bb.iloc[2:3,2:3]Out[36]: three44 9bb.iloc[1:3,1:3]Out[37]: two three33 5 644 8 9bb.iloc[0,0]Out[38]: 1
下面是證明:
bb.iloc[0:4,0:2]=[[9,9],[9,9],[9,9]]In [45]: bbOut[45]: one two three22 9 9 333 9 9 644 9 9 9
三、在原有的DataFrame上新建一個columns或幾個columns
1、什么都不用的,只能單獨創建一列,多列并不好使,親測無效:
bb['new']=[2,3,4]bbOut[51]: one two three new22 9 9 3 233 9 9 6 344 9 9 9 4bb[['new','new2']]=[[2,3,4],[5,3,7]]KeyError: "['new' 'new2'] not in index"
賦予的list基本就是按照所給index值順序賦值,可是一般我們是要對應的index進行賦值,想要更高級的賦值就看后面的了。
2、使用字典進行多列按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]: bbOut[59]: one two three new hi hello ok22 9 9 3 2 33 55 45733 9 9 6 3 234 44 5544 9 9 9 4 657 77 77這里aa是一個字典和列表的嵌套,相當于一條記錄,使用keys當做index名而不是一般默認的columns名。達到了按index多列匹配的目的。由于dict()儲存是混亂的,之間用dict()而不給他的index賦值會記錄錯亂,這一點注意值得注意。
四、刪除多列或多記錄:
刪除列
bb.drop(['new','hi'],axis=1)Out[60]: one two three hello ok22 9 9 3 55 45733 9 9 6 44 5544 9 9 9 77 77
刪除記錄
bb.drop([22,33],axis=0)Out[61]: one two three new hi hello ok44 9 9 9 4 657 77 77
跟大家分享一篇關于python中pandas.DataFrame對行與列求和及添加新行與列示例,感興趣的朋友們可以看看。
DataFrame還有很多功能還沒有涉及,等以后有涉及到,看完官網的API之后,還會繼續分享,everything is ok。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。
新聞熱點
疑難解答
圖片精選