DataFrame的這些操做和Series很類似,這裏簡單介紹一下。html
apply()函數應用於軸級別,applymap應用於元素級別:正則表達式
DataFrame.apply(self, func, axis=0, raw=False, result_type=None, args=(), **kwds)
DataFrame.applymap(self, func)
定義一個函數fun,使用apply()函數把fun應用到由DataFrame對象的列構成的一維數組上,一般fun函數是由聚合函數構成的。數據庫
f=lambda x: x.max()-x.min df.apply(f)
定義一個函數foo,使用applymap()函數把函數foo應用於DataFrame對象的各個元素上,數組
foo=lambda x: '%.2f' % x df.applymap(foo)
轉換數據,調用函數對循環對數據元素進行處理:app
DataFrame.transform(self, func, axis=0, *args, **kwargs)
追加是增長數據框的數據,截斷是把數據從數據框中刪除。函數
1,追加spa
向數據框的末尾追加數據行:rest
DataFrame.append(self, other, ignore_index=False, verify_integrity=False, sort=None)
也就是把一個結構相同的DataFrame追加到另外一個DataFrame的後面,把兩個DataFrame合併爲一個。code
2,截斷orm
能夠按照行索引來截斷數據,也能夠按照列索引來截斷數據:
DataFrame.truncate(self, before=None, after=None, axis=None, copy=True)
參數註釋:
數據框的鏈接操做相似於關係型數據庫中的JOIN操做。
1,天然鏈接
兩個數據框按照on條件進行鏈接,或按照索引,或按照同名的字段進行鏈接,按照等值條件進行匹配:
DataFrame.join(self, other, on=None, how='left', lsuffix='', rsuffix='', sort=False)
參數註釋:
2,合併
相似於關係型數據庫的鏈接操做,和join函數的功能相同,按照等值條件進行匹配,可是用法比join函數更靈活:
DataFrame.merge(self, right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None)
參數註釋:
重複值是指同一列中重複出現的值。
1,檢測重複值
函數duplicated用於檢測DataFrame的列中是否存在重複值,
DataFrame.duplicated(self, subset=None, keep='first')
subset:列標籤,或列標籤序列
keep:有效值是‘first’, ‘last’, False, default ‘first’
2,刪除重複值
drop_duplicates()刪除包含重複值的數據行
DataFrame.drop_duplicates(self, subset=None, keep='first', inplace=False)
subset:列標籤,或列標籤序列
keep:有效值是‘first’, ‘last’, False, default ‘first’
重索引的目的是使原始索引按照新的索引進行排序
DataFrame.reindex(self, labels=None, index=None, columns=None, axis=None, method=None, copy=True, level=None, fill_value=nan, limit=None, tolerance=None)
參數註釋:
重命名軸的標籤和軸的name屬性
1,重名軸的標籤
重命名軸的標籤,傳遞的參數是dict-like對象,key和value是1-to-1的,key表示原始標籤,value表示新標籤:
DataFrame.rename(self, mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors='ignore')
參數註釋:
2,重命名軸的name屬性
軸有name屬性,使用rename_axis()重命名軸的name屬性:
DataFrame.rename_axis(self, mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False)
參數註釋:
mapper:一般狀況下是標籤紙,表示軸的新name
重置索引,默認使用整數索引代替原始索引:
DataFrame.reset_index(self, level=None, drop=False, inplace=False, col_level=0, col_fill='')
參數註釋:
例如,建立多級的行索引和列索引,以下所示,行索引是2級別的,兩個級別的名稱分別是class和name;列索引也是2級的,列索引兩個級別都是匿名的。
>>> index = pd.MultiIndex.from_tuples([('bird', 'falcon'), ... ('bird', 'parrot'), ... ('mammal', 'lion'), ... ('mammal', 'monkey')], ... names=['class', 'name']) >>> columns = pd.MultiIndex.from_tuples([('speed', 'max'), ... ('species', 'type')]) >>> df = pd.DataFrame([(389.0, 'fly'), ... ( 24.0, 'fly'), ... ( 80.5, 'run'), ... (np.nan, 'jump')], ... index=index, ... columns=columns) >>> df speed species max type class name bird falcon 389.0 fly parrot 24.0 fly mammal lion 80.5 run monkey NaN jump
把class級別的行索引轉換爲列,插入到列索引的級別1中:
>>> df.reset_index(level='class', drop=False, col_level=1) speed species class max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
查看DataFrame的多級列,新插入的列'Class'在另外一個level上是沒有列名的,這能夠經過設置col_fill參數來爲另外一個level上的列名賦值:
>>> df.reset_index(level='class', drop=False, col_level=1).columns MultiIndex([( '', 'class'), ( 'speed', 'max'), ('species', 'type')], )
修改新加的列在其餘level的列名:
>>> df.reset_index(level='class', col_level=1, col_fill='genus') genus speed species class max type name falcon bird 389.0 fly parrot bird 24.0 fly lion mammal 80.5 run monkey mammal NaN jump
設置軸的索引,可使用set_index()函數把已有的列轉換爲行索引,也可使用set_axis()函數替換掉已有的軸索引。
1,把已有的列轉換爲行索引
使用現有的列做爲DataFrame的索引:
DataFrame.set_index(self, keys, drop=True, append=False, inplace=False, verify_integrity=False)
參數註釋:
2,替換原始索引
把給定的軸的索引替換爲新索引:
DataFrame.set_axis(self, labels, axis=0, inplace=None)
參數註釋:
使用新的索引來替換原始的行索引:
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) >>> df.set_axis(['a', 'b', 'c'], axis='index', inplace=False) A B a 1 4 b 2 5 c 3 6
選擇是指經過軸索引來選擇數據,而過濾是指按照軸標籤(徹底匹配、模糊匹配和正則匹配)來過濾數據。
1,選擇
選擇Top N行、尾部N行、或者從給定的位置處返回數據:
DataFrame.head(self, n=5) DataFrame.tail(self, n=5) DataFrame.take(self, indices, axis=0, is_copy=True, **kwargs)
舉個例子,返回從行索引爲0和3的數據行:
df.take([0, 3])
2,過濾
也可使用正則表達式、模糊匹配等方式,從數據框的軸標籤中匹配值,返回數據:
DataFrame.filter(self, items=None, like=None, regex=None, axis=None)
參數註釋:
參考文檔: