python replace_DF.replace介绍
生活随笔
收集整理的這篇文章主要介紹了
python replace_DF.replace介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
介紹
replace是python.pandas包下DataFrame中一個數據替換的方法。
用法
pandas.DataFrame.replace
DataFrame.replace(to_replace=None,value=None,inplace=False,limit=None,regex=False,method='pad')替代范圍:
str, regex, list, dict, Series, int, float, or None
實例
數值取代
>>> s = pd.Series([0, 1, 2, 3, 4]) >>> s.replace(0, 5) 0 5 1 1 2 2 3 3 4 4 dtype: int64>>> df = pd.DataFrame({'A': [0, 1, 2, 3, 4], ... 'B': [5, 6, 7, 8, 9], ... 'C': ['a', 'b', 'c', 'd', 'e']}) >>> df.replace(0, 5)A B C 0 5 5 a 1 1 6 b 2 2 7 c 3 3 8 d 4 4 9 e列表取代
>>> df.replace([0, 1, 2, 3], 4)A B C 0 4 5 a 1 4 6 b 2 4 7 c 3 4 8 d 4 4 9 e>>> df.replace([0, 1, 2, 3], [4, 3, 2, 1])A B C 0 4 5 a 1 3 6 b 2 2 7 c 3 1 8 d 4 4 9 e>>> s.replace([1, 2], method='bfill') 0 0 1 3 2 3 3 3 4 4 dtype: int64字典取代
>>> df.replace({0: 10, 1: 100}) A B C 0 10 5 a 1 100 6 b 2 2 7 c 3 3 8 d 4 4 9 e df.replace({'A': {0: 100, 4: 400}})A B C 0 100 5 a 1 1 6 b 2 2 7 c 3 3 8 d 4 400 9 e表達式取代
>>> df = pd.DataFrame({'A': ['bat', 'foo', 'bait'], ... 'B': ['abc', 'bar', 'xyz']}) >>> df.replace(to_replace=r'^ba.$', value='new', regex=True)A B 0 new abc 1 foo new 2 bait xyz>>> df.replace({'A': r'^ba.$'}, {'A': 'new'}, regex=True)A B 0 new abc 1 foo bar 2 bait xyz>>> df.replace(regex=r'^ba.$', value='new')A B 0 new abc 1 foo new 2 bait xyz>>> df.replace(regex={r'^ba.$': 'new', 'foo': 'xyz'})A B 0 new abc 1 xyz new 2 bait xyz>>> df.replace(regex=[r'^ba.$', 'foo'], value='new')A B 0 new abc 1 new new 2 bait xyz詳情見鏈接:
pandas.DataFrame.replace - pandas 0.24.2 documentation?pandas.pydata.org總結
以上是生活随笔為你收集整理的python replace_DF.replace介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: redhat安装wine教程_可能是最漂
- 下一篇: chromium浏览器_Chromium